Created
August 27, 2024 02:15
-
-
Save wolfspider/662908fd629262ace94dbc647898f5c5 to your computer and use it in GitHub Desktop.
More code alchemy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using Underscores | |
| using Transducers | |
| using WebAssemblyCompiler | |
| function fpwasm(f) | |
| z = Iterators.countfrom() |> Map(x -> 2x) |> TakeWhile(x -> x < 10) | |
| j = reduce(+, z) | |
| return j | |
| end | |
| compile((fpwasm, Float64); filepath = "fpwasm.wasm", validate = true, optimize = false) | |
Author
Author
After working with FP for a while now I am thrilled that this can be done in such a straightforward way. Truly, without any extra library imports or anything we can see what the machine sees. We see the "while" in TakeWhile and we see the 2x in there as well as the countfrom(). In terms of authoring your own FP language this could be really useful if you do not want to implement everything in a higher level language but want the same guarantees.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The really cool thing is we get the right answer. Now, keep in mind that we need to "reduce" the answer each time. There is no direct translation to a function which eventually executes in terms of writing the IR. The answer is always 20. So now with the marvel of AI we can ask it to simplify the code and make it more readable. After doing this then we get this: