https://kotlinlang.org logo
j

Jason5lee

05/19/2021, 10:15 AM
Is there an unfold equivalent in Kotlin? F# definition:
Copy code
val unfold: 
   generator: 'State -> option<'T * 'State> ->
   state    : 'State 
           -> seq<'T>
t

Tobias Berger

05/19/2021, 10:45 AM
I'm not very familiar with F#. but I guess you're looking for generateSequence
j

Jason5lee

05/19/2021, 11:15 AM
I want a state.
🍿 1
s

Stephan Schroeder

05/19/2021, 11:29 AM
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

Tobias Berger

05/19/2021, 11:34 AM
@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

Stephan Schroeder

05/19/2021, 11:37 AM
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

Nikky

05/19/2021, 5:10 PM
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
78 Views