raulraja
02/20/2020, 12:08 PMtschuchort
03/15/2020, 10:41 PMJakub Pi
03/19/2020, 3:41 PMpakoito
03/25/2020, 1:17 AMJakub Pi
03/25/2020, 3:21 AMstructure.map {data -> analyse(data, structure)}.zip(structure.map{ it.reference}).filter{ it.first.isNotEmpty() }.forEach(::println)
And here's what I have inside the function (without any of the functional predicates/fold):
val identical = structure.filter {
data.size == it.size &&
it != data &&
it.hash == data.hash
}.map { data ->
Record(data, EnumSet.of(Similarity.IDENTICAL))
}
The last step would be to merge the 6-tuples so that the resulting EnumSet has 0-6 elements. Also, what is the alternative to the zip which necessitates creating a lot of unnecessary data.. I guess I would have to change my signature?pakoito
03/25/2020, 3:34 AMSo I want to minimize both the necessary calculations as well as the traversals, creating no intermediate data structures.Sequence it is then, or plain old imperative code
dnowak
03/27/2020, 3:56 PMpakoito
03/27/2020, 9:28 PMmyThing
.flatTap { element: String -> IO { log(element) } }
.flatMap { element: String -> continueProgram(element) }
wakingrufus
03/30/2020, 3:05 PMalso
scope function works well to encapsulate side effectsMichael Friend
04/02/2020, 2:52 PMfun foo(b: Bar) : Type? {
val filtered = b.listProp.filter { // Condition }
if (filtered.isEmpty()) {
// Create and post analytics event
return null
}
val unique = filtered.singleOrNull() ?: run {
// Create different analytics event
return null
}
... // More operations that each can fail and should post an analytics event
return t
}
tschuchort
04/18/2020, 11:08 AMJakub Pi
04/23/2020, 3:39 PMfun <B> foldRightViaFoldLeft(identity: B, f: (A) -> (B) -> B): B =
this.reverse().foldLeft(identity) { x -> { y -> f(y)(x) } }
FP in Kotlin is still in Early Access so the solutions aren't yet available on their github page. But this is what I came up with (using an identity function and composition):
fun <A, B> foldRight2(xs: List<A>, z: B, f: (A, B) -> B): B = foldLeft(xs, {y : B -> y}) { g, a -> {x : B -> f(a, g(x))}}(z)
I've basically used foldLeft to build up a function that will evaluate in the same order as an equivalent foldRight and then executed it with the original identity value. Is this close to the accepted solution or are there downsides to this approach?Jakub Pi
05/11/2020, 2:45 PMraulraja
05/21/2020, 10:20 AMJP
05/25/2020, 10:48 AMraulraja
06/26/2020, 2:28 PMhorse_badorties
07/08/2020, 9:25 AMhorse_badorties
07/09/2020, 8:58 AMTristan Hamilton
12/01/2020, 4:46 PMinline
the lambda. Any reasons why you may want to write it this way over a receiver?
fun myScopedIntFunction(block: MyCustomIntFunctionScope): Int {
return block.receive()
}
fun interface MyCustomIntFunctionScope {
fun receive(): Int
fun Int.plusThree(): Int {
return this + 3
}
}
fun main(){
myScopedIntFunction {
1.plusThree() //won't compile
}
object: MyCustomIntFunctionScope {
override fun receive(): Int {
return 1.plusThree()
}
}.receive() //works
}
Vladyslav Sitalo
12/15/2020, 8:04 PMfun manyParamFun(p1: Int, p2: String, p3: List<String>): String = ...
val newFun = bind(::manyParamFun, p1=5) // to get (String, List<String>) -> String function
I know we can do things like
fun newFun(p2: String, p3: List<String>): String = manyParamFun(5, p2, p3)
but I’m looking for a nicer way..ursus
02/21/2021, 7:04 PMfun FooMapper(netFoo: NetFoo, quaxMapper: (NetQuax) -> Quax): Foo {
val meh = MehMapper(foo.meh, quaxMapper)
val bar = BarMapper(foo.bar)
return Foo(meh, bar)
}
fun MehMapper(netMeh: NetMeh, quaxMapper: (NetQuax) -> Quax): Meh {
quaxMapper(netMeh.quax)
...
}
Sandeep Chandra
04/06/2021, 9:29 AMI have just started looking at Kotlin, I have been using F# on .NET for a while now and I am liking Kotlin. I am missing pipe operator in Kotlin though, is there any plan to bring the pipe operator to Kotlin?
Uberto Barbini
04/10/2021, 12:32 PMAsq
05/07/2021, 3:19 PMVitaliy Zarubin
05/13/2021, 3:57 PMGiovan
09/10/2021, 2:27 PMsealed class
) and pattern matching (when
) to enable functions to match different types to complete different tasks.
This makes most of my functions need to do a pattern matching on the type, which makes me feel a little clumsy, and if I need to add a new type, I need to add a case to each of these functions, it is easy to miss the new case.
The following is a fictitious code example, assuming it is a sticky note model.
sealed class Resource {
data class Note(val content: String?) : Resource()
data class Tag(val name: String?) : Resource()
data class Character(val name: String?) : Resource()
data class Address(val latitude: Float?, val longitude: Float?) : Resource()
}
fun pullResource(): List<Resource> = TODO()
fun Resource.toDecrypt(): Resource = when (this) {
is Resource.Note -> TODO()
is Resource.Tag -> TODO()
is Resource.Character -> TODO()
is Resource.Address -> TODO()
}
fun Resource.checkLegal(): Boolean = when (this) {
is Resource.Note -> content != null
is Resource.Tag -> name != null
is Resource.Character -> name != null
is Resource.Address -> latitude != null && longitude != null
}
inline fun <reified T> List<T>.insert() {
when (T::class) {
Resource.Note::class -> TODO("batch insert into the Note Entity")
Resource.Tag::class -> TODO("batch insert into the Tag Entity")
Resource.Character::class -> TODO("batch insert into the Character Entity")
Resource.Address::class -> TODO("batch insert into the Address Entity")
}
}
fun insertDB(l: List<Resource>) {
fun <T : Resource> List<Resource>.cast(): List<T> = this as List<T>
l.groupBy {
when (it) {
is Resource.Note -> "Note"
is Resource.Tag -> "Tag"
is Resource.Character -> "Character"
is Resource.Address -> "Address"
}
}.let {
it["Note"]?.cast<Resource.Note>()?.insert()
it["Tag"]?.cast<Resource.Tag>()?.insert()
it["Character"]?.cast<Resource.Character>()?.insert()
it["Address"]?.cast<Resource.Address>()?.insert()
}
}
fun main() {
pullResource().map(Resource::toDecrypt).filter(Resource::checkLegal).let(::insertDB)
}
I don’t know if this is right, it feel weird to me.
If in OO style, I thought maybe i can define an interface for Resource, and implement the interface when adding cases. The code is clean and does not miss new cases.Uberto Barbini
09/22/2021, 6:00 AMMilse113
11/16/2021, 6:51 PMbbaldino
12/09/2021, 8:52 PMResult
types. I have a method like this:
fun one(): Result<SomeType> {
return two()
.map { someOtherType ->
three(someOtherType)
.map { someType ->
doSomethingWithSomeType(someType)
someType
}
fun two(): Result<SomeOtherType> { ... }
fun three(someOtherType: SomeOtherType): Result<SomeOtherOtherType> { ... }
the compiler complains that I’m returning Result<Result<SomeType>>
instead of Result<SomeType>
, which I think I get…I just can’t remember the right way combination of `map`/`fold`/something else to “flatten” the result?Sourabh Rawat
12/15/2021, 3:44 PMFlow
of Either
? I want to do a fold
on it as a terminal operator.