| File Crawler

File Information
file/2017-04-01/1490997619_61892c5c25/1490997619_61892c5c25 The Pragmatic Bookshelf | Programming Elixir 1.2

small medium large xlarge

18 Jan 2016, 19:29
Nathan Hornby (5 posts)

I’m struggling to parse this function intended to be passed into the map accessor function get_in/2:

authors = [
  %{ name: "José",  language: "Elixir" },
  %{ name: "Matz",  language: "Ruby" },
  %{ name: "Larry", language: "Perl" }

languages_with_an_r = fn (:get, collection, next_fn) ->
   for row <- collection do
     if String.contains?(row.language, "r") do

IO.inspect get_in(authors, [languages_with_an_r, :name]) 
#=> [ "José", nil, "Larry" ] 

Namely the arguments in the anonymous function. I’m assuming that these macro functions (get_in/2 expect specific arguments in a function passed to them? It seems to be glossed over (unless I’m forgetting something from earlier in the book).

Firstly and most simply what does :get do in this context?

Secondly is anyone able to explain the whole next_fn thing to me? I’m new to the whole functional programming thing (and of course Elixir) so go easy, but this feels like somethings not fully explained?

18 Jan 2016, 19:50
Dave Thomas (389 posts)

Have a look at the documentation for get_in.

It will call a function you pass it with three parameters, The first is always :get, and the last is a function you should call to process the data you extracted.


18 Jan 2016, 21:20
Nathan Hornby (5 posts)

Thanks for the link Dave! That :get seems pretty arbitrary, doesn’t it?

I think I’m still not quite getting the role of next_fn. What does ‘invoked next’ mean in this context; invoked after what? Why’s it being called within the for loop? How’s that nil getting into that list (apposed to it only containing the values that match the if condition)?

I can see that the values that go into it are the one’s that end up in the list, but that’s all I got. Although I’d expect some kind of next_fn.(nil) in an else in that case…

Sorry if what I’m asking seems basic, parts of the puzzle feel missing for me!

18 Jan 2016, 21:30
Dave Thomas (389 posts)

My suggestion: skip this and keep reading.

Some of this stuff is not obvious. Rather than stress now, move on to other cool stuff, but remember that there’s a get_in function for poking around in nested data structures.

Then, six months from now, when you suddenly have a need to do just this, revisit the page. I’m betting it will make more sense, and having a goal will make it easier to apply

18 Jan 2016, 21:44
Nathan Hornby (5 posts)

Haha OK, will do! Thanks for the help and the great book, Dave.

29 Mar 2017, 02:11
David Rodriguez Ayala (1 post)

Source code is here:

:get is arbitrary, yes. But it is required to do the match.

def get_in(data, [h]) when is_function(h),
    do: h.(:get, data, &(&1))
def get_in(data, [h | t]) when is_function(h),
    do: h.(:get, data, &get_in(&1, t))


def get_in(data, [h]),
    do: Access.get(data, h)

The non base case (second function) is a little tricky:
get in my map, if head of list is a function ( in this case ‘languages_with_an_r’)
it sends to ‘languages_with_an_r’ function —>
:get keyword (just to match)
data (the map)
a anon function that looks like this: fn (some_data) -> get_in(some_data, t)
-–> in this particular case, you are sending a single row, and a list that says [:name]

Now, you hit the base case, because there is a single element [h], which is :name :
get in my map, if it is a function (ups!! not in this case, it is an atom)… ok next match
you access the Access.get(data, h) ( in this case Access.get(data, :name) ), say “Jose”

Now, up in the stack, the non base function expands this: &get_in(&1, [:name]) into a name: “Jose”.
So this: h.(:get, data, &get_in(&1, t)), turns out to be a “jose”, apply it to all the collection and you will have a list of names, one of them being nil because the language had no ‘r’ so it never executed any function.

You must be logged in to comment

Eco-coloring and activity books

This packet of books for youth contains 3 coloring and activity books: (1)COLORS OF HEALTH ....(2) DO and LEARN ECO-FUN BOOK....and (3) AMAZING ANCESTORS. There are many pages of fun activities and coloring pages which are also educational.

RFP's, Proposals, and Contracts for Web Designers and Agencies

This full 31 video course you can learn how to increase you income, land more clients, and protect yourself with contracts. 15 bonus down-loadable documents included.

server hosting cell phone accessory discount electronic laptop computer tablet computer game bluetooth monitor headset hard drive

Site Map

Site Map 0  Site Map 1  Site Map 2  Site Map 3  Site Map 4  Site Map 5  Site Map 6  Site Map 7  Site Map 8  Site Map 9  Site Map 10  Site Map 11  Site Map 12  Site Map 13  Site Map 14  Site Map 15  Site Map 16  Site Map 17  Site Map 18  Site Map 19  Site Map 20  Site Map 21  Site Map 22  Site Map 23  Site Map 24  Site Map 25  Site Map 26  Site Map 27  Site Map 28  Site Map 29  Site Map 30  Site Map 31  Site Map 32  Site Map 33  Site Map 34  Site Map 35  Site Map 36  Site Map 37  Site Map 38  Site Map 39  Site Map 40  Site Map 41  Site Map 42  Site Map 43  Site Map 44  Site Map 45  Site Map 46  Site Map 47  Site Map 48  Site Map 49  Site Map 50  Site Map 51  Site Map 52  Site Map 53  Site Map 54  Site Map 55  Site Map 56  Site Map 57  Site Map 58  Site Map 59  Site Map 60  Site Map 61  Site Map 62  Site Map 63  Site Map 64  Site Map 65  Site Map 66  Site Map 67  Site Map 68  Site Map 69  Site Map 70  Site Map 71  Site Map 72  Site Map 73  Site Map 74  Site Map 75  Site Map 76  Site Map 77  Site Map 78  Site Map 79  Site Map 80  Site Map 81  Site Map 82  Site Map 83  Site Map 84  Site Map 85  Site Map 86  Site Map 87  Site Map 88  Site Map 89  Site Map 90  Site Map 91  Site Map 92  Site Map 93  Site Map 94  Site Map 95  Site Map 96  Site Map 97  Site Map 98  Site Map 99  Site Map 100  Site Map 101  Site Map 102  Site Map 103  Site Map 104  Site Map 105  Site Map 106  Site Map 107  Site Map 108  Site Map 109  Site Map 110  Site Map 111  Site Map 112  Site Map 113  Site Map 114  Site Map 115  Site Map 116  Site Map 117  Site Map 118  Site Map 119  Site Map 120  Site Map 121  Site Map 122  Site Map 123  Site Map 124  Site Map 125  Site Map 126  Site Map 127  Site Map 128  Site Map 129  Site Map 130  Site Map 131  Site Map 132  Site Map 133  Site Map 134  Site Map 135  Site Map 136  Site Map 137  Site Map 138  Site Map 139  Site Map 140  Site Map 141  Site Map 142  Site Map 143  Site Map 144  Site Map 145  Site Map 146  Site Map 147  Site Map 148  Site Map 149  Site Map 150  Site Map 151  Site Map 152  Site Map 153  Site Map 154  Site Map 155  Site Map 156  Site Map 157  Site Map 158  Site Map 159  Site Map 160  Site Map 161  Site Map 162  Site Map 163  Site Map 164  Site Map 165  Site Map 166  Site Map 167  Site Map 168  Site Map 169  Site Map 170  Site Map 171  Site Map 172  Site Map 173  Site Map 174  Site Map 175  Site Map 176  Site Map 177  Site Map 178  Site Map 179  Site Map 180  Site Map 181  Site Map 182  Site Map 183  Site Map 184  Site Map 185  Site Map 186  Site Map 187