-
Notifications
You must be signed in to change notification settings - Fork 454
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
Trampoline #77
Conversation
224e4df
to
8f4821a
Compare
Codecov Report
@@ 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
Continue to review full report at Codecov.
|
class TrampolineTest : UnitSpec() { | ||
init { | ||
"trampoline over 10 should return false" { | ||
Trampoline.More { odd(10) }.runT() shouldBe false |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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)) | ||
} | ||
} |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess I should redefine |
Yeah that would give you stack safe semantics and AFAIK is how is also done
in Scala. I plan on adding some tagless final encodings alternatives to
free to show another way to build interpretable program and we can test the
trampolining capabilities there over stack unsafe monads.
…On Sat, Apr 22, 2017, 11:53 AM Jorge Castillo ***@***.***> wrote:
I guess I should redefine Trampoline as a type alias for Free<Function0,
A> now. Is that correct @raulraja <https://github.com/raulraja> ?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#77 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AAb4XKPzcMTCPcM8xOJdBUdsYiHmGE34ks5ryc4GgaJpZM4NEfo8>
.
|
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 |
e9450aa
to
fba3bb2
Compare
Codecov Report
@@ Coverage Diff @@
## master #77 +/- ##
=========================================
Coverage ? 55.47%
Complexity ? 170
=========================================
Files ? 72
Lines ? 1516
Branches ? 181
=========================================
Hits ? 841
Misses ? 596
Partials ? 79
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Excelent!
* improves main docs page * add index to sidebar
* Convert interface to fun interface * Use lambda expression for implementing fun interfaces
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 useTrampoline
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.