Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Functions and collections #113

Merged
merged 9 commits into from
Mar 29, 2023
Merged

Functions and collections #113

merged 9 commits into from
Mar 29, 2023

Conversation

serras
Copy link
Member

@serras serras commented Mar 28, 2023

No description provided.

@serras serras self-assigned this Mar 28, 2023

<!--- INCLUDE

fun fibonacciWorker(n: Int): Int = when (n) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is memoise not valid for DeepRecursiveFunction?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure how they work with each other, since DeepRecursiveFunction has this weird callRecursive.

If you define the memoized version of your function as a `val`, as we've done
above, the cache is shared among **all** calls to your function. In the worst
case, this may result in memory which cannot be reclaimed throughout the whole
execution, so you should apply this technique carefully.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also add something about eviction policies?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe something like:

There's some literature about eviction policies for memoization, but at the moment of writing memoize doesn't offer any type of control over the cached values.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good to me, and perhaps also point them to Aedile.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 5f8c5b2

@nomisRev
Copy link
Member

I'm personally not fond of these APIs in Kotlin. memoise, curry, uncurry & partial as I've shared couple times, and do again as advocate of the devil. Sorry 😬😅

They feel unnatural to me in Kotlin, similar to HKTs but less problematic since they're much less complex, but still simulated into the language. Where Haskell can do this naturally, and Scala has multiple parameter lists, map(sum(_, 1)) for partial application, etc.

Historically they come from funKtionale, whose author has since then left the Kotlin community. Many of the old funKtionale users are now relying on us for continued support of this functionality. It would probably be better served by a compiler plugin, (except memoise), but we've never invested time to do so.

As you already mentioned in the shared link. Caching is one of the hardest issues in software programming, and IMO memoise is a poor-mans solution that is never needed in business applications.

The Fibonacci problem can also be solved by using lazy sequence:

fun fibonacci(): Sequence<Int> =
    generateSequence(Pair(0, 1)) { Pair(it.second, it.first + it.second) }
        .map { it.first }

For this reason I never made work of promoting them on the website. @raulraja originally convinced me of this, not sure how he currently feels about this. We tried removing them in 1.0.0 actually, but were met by resistance from funKtionale users and decided to keep them. Even convincing them to limit it to arity-9 proofed difficult.

@serras
Copy link
Member Author

serras commented Mar 29, 2023

@nomisRev I've done two things to make clear that we're not very fond of partially1, curried, and friends:

  • I've switched the order of appearance in the section: now it's first non-empty collections, then recursion, then utilities.
  • I've added a big :::danger section explaning that we don't consider this idiomatic Kotlin.

@serras serras changed the title [DRAFT] Functions and collections Functions and collections Mar 29, 2023
@serras
Copy link
Member Author

serras commented Mar 29, 2023

It's not a draft anymore, it's turned into a beautiful butterfly ready to review.

Copy link
Contributor

@franciscodr franciscodr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a couple of minor comments. Looks good to me, @serras

@serras serras merged commit cc11648 into main Mar 29, 2023
@serras serras deleted the functions-collections branch March 29, 2023 13:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants