Size: a a a

2019 September 29

ᵛᵉⁿᵈᵉˡⁱᵉᵘ in Kotlin Start
Ребзь, подскажите кто может,
есть массив с n-количеством элементов
мне нужно разложить их по n-штук в массивы
как сделать?

допустим
arrayOf(String....30)
и разложить их по 3 штуки в один array

желаемый результат:
array(String1, String2, String3),
array(String4, String5, String6)
...
источник

ᵛᵉⁿᵈᵉˡⁱᵉᵘ in Kotlin Start
ᵛᵉⁿᵈᵉˡⁱᵉᵘ
Ребзь, подскажите кто может,
есть массив с n-количеством элементов
мне нужно разложить их по n-штук в массивы
как сделать?

допустим
arrayOf(String....30)
и разложить их по 3 штуки в один array

желаемый результат:
array(String1, String2, String3),
array(String4, String5, String6)
...
Ну или как сделать так чтоб data.forEach {}
возвращал не unit а результат выполнения

или подскажите любой метод для подобной реализации, пожалуйста
источник

D

Denys in Kotlin Start
ᵛᵉⁿᵈᵉˡⁱᵉᵘ
Ну или как сделать так чтоб data.forEach {}
возвращал не unit а результат выполнения

или подскажите любой метод для подобной реализации, пожалуйста
data.onEach{}.
источник

D

Denys in Kotlin Start
ᵛᵉⁿᵈᵉˡⁱᵉᵘ
Ребзь, подскажите кто может,
есть массив с n-количеством элементов
мне нужно разложить их по n-штук в массивы
как сделать?

допустим
arrayOf(String....30)
и разложить их по 3 штуки в один array

желаемый результат:
array(String1, String2, String3),
array(String4, String5, String6)
...
data.chunked(3):

fun main() {
   (0..10)
       .map { it }
       .chunked(3)
       .let(::println)
}
источник

MG

Matthew Good in Kotlin Start
is it possible to convert this into a tail recursive function
fun recursion() {
   val funList = mutableListOf<() -> Unit>()
   fun a() {
       // calls funList[1]
       // which calls funList[0]
       // which calls a()
       // thus calling funList[1] again
       funList[1]()
   }
   funList.add { a() } // funList[0]
   funList.add { funList[0]() } // funList[1]
}
источник

D

Denys in Kotlin Start
Matthew Good
is it possible to convert this into a tail recursive function
fun recursion() {
   val funList = mutableListOf<() -> Unit>()
   fun a() {
       // calls funList[1]
       // which calls funList[0]
       // which calls a()
       // thus calling funList[1] again
       funList[1]()
   }
   funList.add { a() } // funList[0]
   funList.add { funList[0]() } // funList[1]
}
What's your goal?
источник

MG

Matthew Good in Kotlin Start
Denys
What's your goal?
to allow for recursion without being limited by the stack space
источник

D

Denys in Kotlin Start
Matthew Good
to allow for recursion without being limited by the stack space
No, I mean what's the goal of this weird functions. :)
источник

MG

Matthew Good in Kotlin Start
as i tried https://adamschoenemann.dk/posts/2019-02-12-trampolines.html but i cant figure out how to call the fib function as if i move it out of the class i get https://pl.kotl.in/n1QekD85x

Unresolved reference: delay
Cannot infer a type for this parameter. Please specify it explicitly.
Unresolved reference: done

and if i try to call it from the class i get

Cannot access '<init>': it is private in 'Trampoline'
Sealed types cannot be instantiated
источник

MG

Matthew Good in Kotlin Start
Denys
No, I mean what's the goal of this weird functions. :)
to create a state to function mapping for a Finite State Machine builder, for example
FiniteStateMachine.state.add(0) {
   println("state 2")
   when (FiniteStateMachine.getInput()) {
       "b" -> FiniteStateMachine.transitionAndExecute(1)
       else -> FiniteStateMachine.transitionAndExecute(3)
   }
}
источник

D

Denys in Kotlin Start
Why do you need recursion here? To solve the problem in fp-way?
источник

MG

Matthew Good in Kotlin Start
what does fp-way mean?
источник

D

Denys in Kotlin Start
Why do you need recursion for state machine?
источник

MG

Matthew Good in Kotlin Start
cus it is possible for it to recursively enter a state
источник

MG

Matthew Good in Kotlin Start
eg 1>2>3>8>1>2>9>1>2>5>1 and so on where the numbers indicate the current state number
источник

MG

Matthew Good in Kotlin Start
tho im not sure if a state machine is tipically allowed to be state recursive depending on its purpose
источник

D

Denys in Kotlin Start
Tailrec usage is limited with certain rules.

> To be eligible for the tailrec modifier, a function must call itself as the last operation it performs. You cannot use tail recursion when there is more code after the recursive call, and you cannot use it within try/catch/finally blocks. 
источник

D

Denys in Kotlin Start
Matthew Good
eg 1>2>3>8>1>2>9>1>2>5>1 and so on where the numbers indicate the current state number
I guess it's much easier to implement this with a state.
источник

MG

Matthew Good in Kotlin Start
Denys
Tailrec usage is limited with certain rules.

> To be eligible for the tailrec modifier, a function must call itself as the last operation it performs. You cannot use tail recursion when there is more code after the recursive call, and you cannot use it within try/catch/finally blocks. 
can it be worked around by using a trampoline instead? (tho i seem to be unable to find such for kotlin)
источник

D

Denys in Kotlin Start
Matthew Good
can it be worked around by using a trampoline instead? (tho i seem to be unable to find such for kotlin)
Unfortunately I'm not familiar with trampolines. I guess we need functional programming expert here.
источник