Since Jan 2014 I’ve been working on Clojure as my “language of the year” to learn. Aside from a brief stint last spring when my company at the time switched to .NET (but then I came back to ruby) I have been keeping at it, learning here and there. Sadly I realized I need to write/read more code and less just reading/watching about it. Bridget Hillyer leads a study group for ladies learning clojure online (we meet in irc) and we want to make a site to help us keep track of recipes as a project to practice clojure. Initially we will just have a list with name, source and url. First versions of the app just created recipes into maps/vector. Now we want to have an API with a database so I started playing with Korma and Liberator these past few weeks. I was wanting a quick way to add a bunch of records for testing and then remove them.
1 2 3 4 5 6 7
Ok I have a function to create a recipe.
Now to add a bunch of recipes
1 2 3 4 5 6 7 8 9
What about a function to add a bunch of data without for
1 2 3 4
Now I wanted to insert the word TEST in front of the name, so I could easily remove the test recipes. I know I could probably switch out databases maybe or something more awesome but just let me play here..
1 2 3 4 5 6 7 8
When I wrote it, I knew it was looooong and not the best way. But blame my
ruby-make-it-work-refactor-rinse-repeat roots but hey it works. Now to make it
Super excited, I show Bridget at our weekly study group meeting. I told her I
know there is a one liner, but I got this one working.. I’m trying
but can’t get it quite, she said look at
map .. you’ll need a couple of maps.
So that was enough of a hint to set me in the right direction.
Starting with a simpler example and playing in the repl, I came up with:
1 2 3 4 5
Thinking the road to completion of this function was near, I attempt to apply this to my map structure with all the info, hmm hmm hmm. Was tough to get it all in on function. So, I decided to split it up. Adding TEST to one recipe, then applying that to a vector of recipes.
I pull out the name from the data, made a new map with the modified name then smooshed them together returning a new data structure with merge.
Then to apply that to a bunch of data, then add all of them (maybe this is not necessary to be in its own function but I’m getting the hang of map here:
1 2 3 4
Ok thats was fun.
1 2 3 4 5 6
But how I arrived at these was a learning process. Thanks to Bridget for the hint to use map. :)
Any suggesting on writing it differently? I’d lean towards readability over perl golf anyday!
I know I barely scratched the surface of map but its more than I knew before, so progress! :)
Update: Got a great suggestion from @stijlist