Is there an unfold equivalent in Kotlin? F# defini...
# announcements
j
Is there an unfold equivalent in Kotlin? F# definition:
Copy code
val unfold: 
   generator: 'State -> option<'T * 'State> ->
   state    : 'State 
           -> seq<'T>
t
I'm not very familiar with F#. but I guess you're looking for generateSequence
j
I want a state.
🍿 1
s
can you be more specific? l - like Tobias - can't read F#. It's too for me foreign to make sense of it.
Copy code
val x = 1
is already a state, but your
unfold
seems way more elaborate.
t
@Jason5lee I'm also not sure what you mean with state? Is it about having the last value available in the lambda for creating the next value?
generateSequence
can do that, just look at the second example on the page I linked above. If you mean something else, please elaborate.
s
Copy code
Unfolding

the opposite of a fold; taking a single value and turning it into a list, or "unfolding" it.
definititly sounds like generating a sequence to me.
n
so something like so?
Copy code
fun countDown(n: Int) = generateSequence(n) { n ->
  if(n > 0) n-1 else null
}
yep.. thats what generateSequence can do
196 Views