Hello! I am looking for something like this in Kot...
# arrow
a
Hello! I am looking for something like this in Kotlin Arrow. How can I accomplish the same with a list of
Some
values?
j
For the image you posted we have the equivalent
Option<A>.toList(): List<A>
. For a list of
Some
values it depends, do you want behaviour like
catMaybe
(http://hackage.haskell.org/package/base-4.12.0.0/docs/Data-Maybe.html#v:catMaybes) or do you want to short-circuit and return
Nothing
when you encounter a
Nothing
?
a
You are right, I was looking for
catMaybe :: [Maybe a] -> [a]
. Do we have a function in Arrow that does that? The team I work with found
filterMap
, I wonder if that's what we should use.
p
yep
a
Why not
catSome
or something along that line? I would have never thought to use
filterMap
, besides, this looks a bit awkward:
filterMap { it }
.
p
catSome is specialised, we’re trying to stay away from those
filterMap(::identity) is legitimate
r
@Attila Domokos I think
catSome
as an alias for
filterMap
+
identity
is legit because it’s understood that filterMap short circuits on None. @pakoito this is similar to the traverse and sequence case.
If we have
traverse(identity)
why do we need
sequence
, it’s just a convenience for most users IMO
p
then we just need a name
a
filterMap
+
identity
is legit because it’s understood that filterMap short circuits on None.
I did not know that. This is in the arrow docs:
filterMap fun <B> filterMap(f: (A) -> Option<B>): Option<B>
Should I add a bit more context to the docs here? https://arrow-kt.io/docs/apidocs/arrow-core-data/arrow.core/-option/ I was also thinking about having a function like
toValues
that could be used in an abstract way for
Some
and
Either
as well. Maybe that would help others finding this functionality.
p
it's
coflatMap
, we were actually checking the implementation with @Jannis yesterday 😄
j
I think the term short-circuit was misused. From my understanding short-circuit means it aborts the outer function and immediatly returns.
traverse
shorts on
None
filterMap
completes, but removes all
None
cases.
a
I could live with that, if it's easily found in the docs. This is not very helpful:
fun <B> filterMap(f: (A) -> Option<B>): Option<B>
I did not even see a list in this function signature.
j
isn't filterMap on functorfilter or am I mistaking something here?
a
Oh my
[Maybe a] -> [a]
is just so much more descriptive.
Good point on this:
From my understanding short-circuit means it aborts the outer function and immediatly returns.
j
Ah here:
fun <A, B> Kind<F, A>.filterMap(f: (A) -> Option<B>): Kind<F, B>
is on
FunctorFilter
which most collections that we support implement. We also have
fun <A> Kind<F, Option<A>>.flattenOption(): Kind<F, A> = filterMap(::identity)
which for lists has the signature
fun <A> Kind<ForListK, Option<A>>.flattenOption(): Kind<ForListK, A>
which is equal to
fun <A> List<Option<A>>.flattenOption(): List<A>
. So we already have this 🙂
@pakoito this still somewhat confuses me 😄 I know how it works, but applying it to different methods and concepts is still confusing 😄
😄 1
a
You might have this, but a user might struggle finding it. I did.
The
ForListK
is definitely telling. 👍
j
Yes, you are absolutly right,
FunctorFilter
does not have that much exposure on the docs (if any)
p
i Had forgotten about that one
hahaha
discoverability is based on IDE looking up extfun for the current context
a
I contributed with my first docs PR, I'll see if I can chip in a bit more. Thanks for not taking my comments the wrong way. You area all doing a great work. 🥇
p
without something like Hoogle it'll be hard to lookup things by the result you want. It's something I proposed as a compiler plugin
a
Yeah,
Hoogle
definitely helps.
p
you write what you expect, click the correct button, and we do a naive lookup through all the functions to see if they match
you may need to fill in some implementation details, still you'll know what functions may get you there
a
I am new to IntelliJ and I wish I wouldn't need to use it. I try to use the docs, it has served me well in the past, in the JVM Universe it's a bit different.
j
Looking at haskell, docs are usually called terrible and lacking. Which is certainly true for introductions and tutorials but discovering functionality is top-notch (thanks to both hoogle and haskells really nice generated docs). The jvm world is slacking in that regard, javadoc/kdcoc exist, but are ugly and painful to navigate... Having a hoogle alternative would be amazing 🙏
💯 1
r
We can build hoogle in Kotlin easily with meta
we need a use case section with ideas
I’ll be happy to help anyone that wants to take on this with meta find what needs to happen to index all functions
it’s more interesting to produce an Index that can be used anywhere as that also would work in the browser in the site
as we replace the Algolia search
a
You should be able to express things simply for search, though:
[Maybe a] -> [a]
is super sweet.
KindOf(ListOfK of (ListtOfK of something)) -> KindOf(ListOfK of somethingElse)
is a lot more work.
j
Can you hook into the intellij search? (The one that comes up with default keybinds shift + shift?) Regarding the index, as a secondary goal, it should be extensible based on dependencies, not sure how, but having third-party libraries indexed would be great
we can easily parse basic haskell code as well and translate it I think
😍 1
p
we need a use case section with ideas
it's there 😄
j
So I think what we need is a project like hoogle, which given any kotlin library can index and lookup functions which we can then use in meta to drive a search helper. Just to gauge how hard this is, this needs a way to extract the entire public api from a project, index it in some optimized structure and implement a basic search algorithm (ses, a* etc). Implementing the algorithms is easy, neither ses nor a* are that hard. I think the most difficult part is going to be either parsing the api or finding a decent datastructure to represent it. Thoughts?
Anyway, even the simplest version of this is probably a lot of work, if anyone starts something like this let me know, I'd like to help
r
I'll take anyone interested to the point where you are post analysis and have access to all functions in the classpath. All after that is generating a text file in a common format that can be consumed elsewhere. We'll wire afterward the tooling on the site with the 47 frontend team
I'll provide the meta skeleton and guide to extract the info we need out of the function
Essentially we need a map of the polymorphic version of each function to the concrete one so when the user types for a curried shaped.
They get all matching functions
This can also be built as a plugin for dokka with meta so everyone gets it, not just arrow
But the basic meta plugin should be simple
j
You are making this really tempting... If it's still open around february (after exams :/) I might give this a try 🙂
r
Thanks @Jannis!