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

Trampoline #77

Merged
merged 9 commits into from
Jul 25, 2017
Merged

Trampoline #77

merged 9 commits into from
Jul 25, 2017

Conversation

JorgeCastilloPrz
Copy link
Member

@JorgeCastilloPrz JorgeCastilloPrz commented Apr 21, 2017

Fixes #78

Trampoline is often used to emulate tail recursion. The idea is to have some step code that can be trampolined itself to emulate recursion. The difference with standard recursion would be that there is no need to rewind the whole stack when we reach the end of the stack, since the first value returned that is not a trampoline would be directly returned as the overall result value for the whole function chain. That means Trampoline emulates what tail recursion does.

There are languages that do not have tailrec modifier that use Trampoline to emulate the same behavior. But Trampoline is also useful for languages that also have trampolines out of the box:

"As long as you use tail recursion with one function everything is fine as the compiler will optimize it for you and transform it in a loop, issues come when you are trying to use tail recursive calls between two functions or when you don’t have a tail recursive call."

Trampolines are stack safe, since they do not need to maintain a function call stack (since they don't need to rewind it). So trampolines are useful also when you don't want to blow your stack up for too long recursive operations.

@codecov
Copy link

codecov bot commented Apr 21, 2017

Codecov Report

Merging #77 into master will increase coverage by 0.34%.
The diff coverage is 71.42%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master      #77      +/-   ##
============================================
+ Coverage     41.94%   42.28%   +0.34%     
- Complexity       81       84       +3     
============================================
  Files            40       41       +1     
  Lines           720      726       +6     
  Branches        124      126       +2     
============================================
+ Hits            302      307       +5     
  Misses          373      373              
- Partials         45       46       +1
Impacted Files Coverage Δ Complexity Δ
...rc/main/kotlin/katz/instances/NonEmptyListMonad.kt 80% <62.5%> (ø) 0 <0> (ø) ⬇️
katz/src/main/kotlin/katz/data/Trampoline.kt 83.33% <83.33%> (ø) 3 <3> (?)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 51bb27c...e9450aa. Read the comment docs.

class TrampolineTest : UnitSpec() {
init {
"trampoline over 10 should return false" {
Trampoline.More { odd(10) }.runT() shouldBe false
Copy link
Member

Choose a reason for hiding this comment

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

Where does runT come from?

Copy link
Member Author

@JorgeCastilloPrz JorgeCastilloPrz Apr 22, 2017

Choose a reason for hiding this comment

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

Inherited from Trampoline, it's a sealed class. What's up?

If you are talking about syntax, saw it on some scala sample on teh internet

}
is Either.Left<*> -> go(buf, f, NonEmptyList.fromListUnsafe(f(v.head.a as A).ev().all + v.tail))
}
}
Copy link
Member

Choose a reason for hiding this comment

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

The changes to this file are just formatting, right?

Copy link
Member Author

Choose a reason for hiding this comment

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

No. Removed the need to return something on the body. I followed Fernando's advice to avoid compilation errors.

Copy link
Member Author

Choose a reason for hiding this comment

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

@JorgeCastilloPrz
Copy link
Member Author

I guess I should redefine Trampoline as a type alias for Free<Function0, A> now. Is that correct @raulraja ?

@raulraja
Copy link
Member

raulraja commented Apr 22, 2017 via email

@JorgeCastilloPrz
Copy link
Member Author

I'm trying to work on the compiler plugin from now on on my spare time, so if anybody has a deeper idea on how to translate Trampoline to be declared in terms of Free it would be nice if he can take the ownership on this PR. Otherwise I'll do it when I'm finished with the plugin / annotation processor , whatever it is, since I still don't know :D

@pakoito pakoito mentioned this pull request Jul 22, 2017
@codecov-io
Copy link

codecov-io commented Jul 25, 2017

Codecov Report

❗ No coverage uploaded for pull request base (master@98f407e). Click here to learn what that means.
The diff coverage is 66.66%.

Impacted file tree graph

@@            Coverage Diff            @@
##             master      #77   +/-   ##
=========================================
  Coverage          ?   55.47%           
  Complexity        ?      170           
=========================================
  Files             ?       72           
  Lines             ?     1516           
  Branches          ?      181           
=========================================
  Hits              ?      841           
  Misses            ?      596           
  Partials          ?       79
Impacted Files Coverage Δ Complexity Δ
kategory/src/main/kotlin/kategory/free/Free.kt 58.97% <100%> (ø) 1 <0> (?)
...tegory/src/main/kotlin/kategory/data/Trampoline.kt 60% <60%> (ø) 0 <0> (?)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 98f407e...7a1055d. Read the comment docs.

Copy link
Member

@raulraja raulraja left a comment

Choose a reason for hiding this comment

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

Excelent!

@raulraja raulraja merged commit 317104a into master Jul 25, 2017
@raulraja raulraja deleted the add-trampoline branch July 25, 2017 13:43
@raulraja raulraja mentioned this pull request Jul 25, 2017
54 tasks
ambrusadrianz pushed a commit to ambrusadrianz/arrow that referenced this pull request Oct 22, 2019
* improves main docs page

* add index to sidebar
rachelcarmena pushed a commit that referenced this pull request Feb 24, 2021
rachelcarmena pushed a commit that referenced this pull request Feb 24, 2021
* Convert interface to fun interface

* Use lambda expression for implementing fun interfaces
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.

4 participants