#Language Tour Question

1 messages · Page 1 of 1 (latest)

wintry knot
#

I'm going through the Gleam Language Tour and noticed something that confused me.
On the https://tour.gleam.run/standard-library/list-module/ page there is an example for fold that goes as follows: io.debug(list.fold(ints, 0, fn(count, e) { count + e }))
Shouldn't that be: io.debug(list.fold(ints, 0, fn(e, count) { count + e })) because ints maps to iterating over e and count is the running total? The result is the same in this case, but might not always be?

An interactive introduction and reference to the Gleam programming language. Learn Gleam in your browser!

solemn cave
#

Gleam is a data-first language

#

It means you can do this

#
list.fold(items, set.new(), set.insert)
slate blaze
wintry knot
#

Right, so because pipelines and such (data-first) it's just flipped, so it reads as "For each item in this list, starting at X do Y" but in actuality it runs as "Starting at X, for each item in this list do Y"

solemn cave
#

I don't know what you mean, sorry

wintry knot
#

You pass in 3 arguments into fold(), the list (aka "for each item in this list" in my previous message), the starting point/initial (aka "X" in my previous message) and the function (aka "Y" in my previous message). So you say list.fold(list[1, 2], X, Y(X, List[1, 2])) which then runs Y(Y(X, 1), 2). So although your first parameter for calling list.fold() is list[1, 2], in this case, it's passed in as the second parameter into Y (and the same for X being passed in as the second argument to list.fold() but is the first parameter when it's passed into fn Y(). Does that make any sense? 😄

#

I feel like I'm basically rewriting the docs that Jak posted, but in a confusing manner

#
  over list: List(a),
  from initial: b,
  with fun: fn(b, a) -> b,
) -> b``` 
All I'm trying to confirm is that although you pass in `List(a)` __first __into `list.fold()` , it's being used as the __second __ parameter for the function you're folding with.
#

Therefore io.debug(list.fold(ints, 0, fn(count, e) { count + e })) is correct because ints is being mapped to e and 0 is being mapped to count

solemn cave
#

Aye, because the data goes first

#

The data of list.fold is the list

#

the data of the reducer is the accumulator

#

They have different subjects

#

This is consistent throughout the Gleam ecosystem

wintry knot
#

#

Thanks for your time and help 🙂

solemn cave
#

thank you thank you

wintry knot
#

Do I have to mark it as solved or.. ?