Sunday, May 17, 2015

Intro to Higher Order Functions

"Plucking the grass to know where sits the wind"
-- Shakespeare, The Merchant of Venice
Act I, Scene I, Line 19


Step on up.  Come one, come all. See the amazing function which produces other functions.  See the marvelous function which consumes other functions.  See things which you will tell your grandkids about.

Functions Producing Functions

Our first sight is the amazing function which produces other functions.

function A produces function B

In JavaScript and Clojure we would have the following:

We see in repl.js that we are having a return b which returns the string 'Bilbo'.  Likewise in repl_es6.js we see the first lambda return another lambda which returns the string 'Bilbo'.  In repl.clj we see the function a returns the function b which returns the string "Bilbo".  Three examples of a function producing another function.

Functions Consuming Functions

Our next sight is a function which consumes another function.

function A consumes function B

In JavaScript and Clojure we would have the following:

We see in repl.js that we are having function a consume function b which when invoked returns the string 'Bilbo'.  Likewise in repl_es6.js we see the first lambda consumes the lambda passed in which when invoked returns the string 'Bilbo'.  In repl.clj we see the function a consumes the function b which when invoked returns the string "Bilbo".  Three examples of a function consuming another function.


The next sight we see Pluck taking apart a dwarf.

In JavaScript and Clojure we would have the following:

We see in repl.js and repl_es6.js we can use lodash's Pluck function extract the name property from our dwarves.  Likewise in repl.clj we can use the Map function with the :name keyword to extract the name value form our dwarves.


This last sight to see is a function which will Reduce its input to a single value.

In JavaScript and Clojure we would have the following:

In repl.js and repl_es6.js we can use lodash's Reduce / Foldl function to sum up the values of 1, 2, 3, and 4 to get the value of 10, we can leave out the seed value and still get 10, or we can use a seed value of 100 to get 110.  Likewise in repl.clj we can use the Reduce function to sum up the values of 1, 2, 3, and 4 to get again 10, we can also not give a seed value or given it a different value just as before.


Thank you and watch your step on the way out.

Sunday, April 12, 2015

Rearrangeable FizzBuzz

"What zeal, what fury hath inspired thee now?"
-- Shakespeare, Love's Labour's Lost

I was inspired a while back by the talk "Write the Other Half of Your Program" by Jason Hemann and Daniel Friedman.

In the talk they showed how one could take a program written in Racket and translate it over to miniKanren.  One of the things I found very interesting was rewriting conditional statements in such a way as that they can be reordered and still produce the same result (I believe would eliminate procedural cohesion).

What would an example of this look like?  Let us look at one example with the FizzBuzz kata.

We see in the code above that the conditional statements around when to Fizz, Buzz, FizzBuzz, or just return the value given are more complex than they normally are.

Here is the Fizz condition:

(fizz? x)
(not-buzz? x))

We see that in the Fizz condition we are testing that it is a Fizz and that it is not a Buzz, this will allow us to relocate the this condition before the FizzBuzz condition if we want to, but only if the FizzBuzz condition will allow this (which it will).

Here is the Buzz condition:

(buzz? x)
(not-fizz? x))

We see that it is the reverse of the Fizz.

Here is the FizzBuzz condition:

(buzz? x)
(fizz? x))

We see that we are testing for both Fizz and Buzz conditions.

Finally here the else condition:

(not-fizz? x)
(not-buzz? x))

We see that we are looking for Not Fizz and Not Buzz.  In a sense we have came up with our own little DSL around the logic of FizzBuzz and at the same time have written our conditions in such a way as to allow for them to be completely rearranged!

Saturday, April 4, 2015

Zipping in SQL

"That I am that same wall; the truth is so."
-- Shakespeare, A Midsummer Night's Dream
Act V, Scene I, Line 160

SQL and higher order functions are completely different.  SQL is used to apply functions against collections of data, while higher order functions are used to apply functions against collections of data.  Totally different.

I mean have you ever seen both Michael Fogus and Itzik Ben-Gan talked about in the same blog post?!?

I have no idea why people classify declarative programming with SQL as completely different than declarative programming with higher order functions.

I'll use this blog post as a jumping off point to show that they are very similar things.

Let's look at the higher order function of zip.  The idea of zip is that you take two or more collections of data and zip them together forming one collection of zipped data.  Say you have a collection of greetings ["Hello", "Hi", "Aloha"] and another collection of names ["Mike", "Kelsey", "Jack"].  Given a zip function which takes one string and concats it with ", " and another string you could zip these collections together with that zip function obtaining the following collection ["Hello, Mike", "Hi, Kelsey", "Aloha, Jack"].

Let's look at this in some code.

In the first example we have C# using the zip method on the String class.  For the C# zip, we need two collections and a lambda express.

In the second example we have Clojure using the map function.  For the Clojure map, we need a function and one or more collections (in this example we have two).

In the third example we have T-SQL using an INNER JOIN along with a CONCAT in the SELECT statement.  An INNER JOIN you say?  Yes, an INNER JOIN can be thought of as the same thing as a zip.  The key is that the key you are matching on (in this case the ROW_NUMBER) needs to match.  In the Clojure example we are using a vector which is an associative data structure with the keys being the index values, which is the exact same thing we have in the SQL!

Please play around with the fiddle I created to show this in action.

Sunday, March 29, 2015

How to Attend a Programming Conference

"Cannot induce you to attend my words"
-- Shakespeare, Othello
Act III, Scene III, Line 278

Tis that time of the year.  A time when a developer's thoughts turn to conferences.

The following is about how I attend conferences.  The most important thing to know first is that I attend conferences to learn.  If your goal in attending a conference is to party it up in a fun location then this post is not for you.

First, know what type of conference it is that you are attending.

  • Is it single track or multi track?
  • Is it local, regional, or global?
  • Is run for profit or is it sponsored?
  • Is it a single day or multi day?
  • ...

For the most part when you are attending a conference the only real difference is if it is multi track or single track.  The other questions are all around what type of travel plans you will need to make, how much will it cost, and what type of other activities it will have outside of the sessions.  As such I will just focus on the differences around the number of tracks for the rest of the post.

Single track conferences often have a theme which maybe language, technology, or topic based.  This is great if you want to get really in-depth.  You'll find lots of people who are interested in the same theme.  That being said when you go to this type of conference it will most likely be on you to make the most of it by getting out there and talking to other people.  I have two tips for meeting people at a conference like this:

  • Stand/sit at an empty table during breaks.
    Often people will come over and join you (even if just to find a place to rest their snack), this is a great chance to strike up a conversation.  Remember you have at least one thing in common, you have both just seen the exact same session.
  • Join a table during breaks.
    Some times you will strike out doing this and join a table of friends that will not talk to you much, no big deal just head over to a different table.  Remember you have the conference in common, so it should be easy to find things to talk about.
Multi track conferences are a great way to branch out and learn something completely new.  With a multi track conference you can often find sessions in areas that you only have a little bit of knowledge of.  That being said it is up to you to pick the sessions and make the conference your own. I have a few tips for a conference like this:
  • Everyday go to one session in which you have no knowledge of the topic.
    Multi track conferences are a great way to get a glimpse of another world.  If you are a backend developer, go to an UI design session; if you are a front-end developer go to a session on databases.  You have a great opportunity to learn what other people are dealing with and care about.
  • See "famous" speakers.
    Not all speakers have the same speaking style or are at the same level.  Go see a "famous" speaker even if they are talking about a topic you do not care about.  If the speaker is "famous" because they are good, you'll learn something and who knows you might be able to use that new knowledge latter on.
  • See a first-timer / newer speaker.
    Everyone has their first time that they are doing something.  Uncle Bob was not born an accomplish public speaker.  Go see someone that is new to the scene, they might not be as polished as a "famous" speaker but they might have more in-depth knowledge about their topic.
  • Make sure to ask questions at the sessions.
    There is almost always time at the end of the session to ask questions, do so.  Asking questions is a great way to make sure that you understand what you just saw, it is also a good way to get advise from an expert.
A bit of warning around sponsorship and conferences.  Beware at conferences of speakers that work for sponsors of the conference.  Some of these speakers get their spots based on their own merits and may even get their company to sponsor the conference because they believe in it and want to help even more, but there are times that speakers get their spots because they work for a sponsor, this is not to say that they are a bad speaker or that the session will be a sales pitch, just beware and know that you might be getting a sales pitch in your session.

I hope you find these tips useful and if you see me at a conference stop by and say hi.

Sunday, March 22, 2015

Vectors and Maps are Only Different in Your Mind

"That roars so loud and thunders in the index?"
-- Shakespeare, Hamlet

The other day I found something interesting while reading Clojure Programming.

"While it might be counterintuitive, maps and vectors are both associative collections, where vectors associate values with indices."
-- Clojure Programming, page 100

This is yet another case of the book, Clojure Programming, stating something that I thought of but was unable to adequately express in words.
The differences placed around data structures are often either just in our minds or a condition you place on the structure its self.

In the case of the structures just being different in our minds we can look at the example of a map and vector in Clojure.

We see in the example above that we could use a vector as a map where the indices are the keys.  We see in the first example (get v 1) that we are just using the index of 1 to get the second value from the vector (indices are zero based in Clojure).  With the second example (get v 99 :not-found) we see that we can assign a default value to be return when a value is not found.  In the last vector example we see that we can associate new values in the vector resulting in a different vector being returned (do not worry vectors are immutable in Clojure).  In the map examples we see we can treat a map like a vector by using integers for the keys.

In the second case of data structures being different due to a condition imposed on them, we do not have to look an further than our example of a map.

(assoc m
1 "Mr. Jack"
0 2002
2 :good-boy)
;; {0 2002, 1 "Mr. Jack", 3 4, 2 :good-boy}

We see a map is unordered in Clojure.  If you want to have the data be stored in an order then you would want to use a sorted-map instead of a hash-map.