-
-
Save getify/3542996ab54b5be2a648ecfcb6bb6bc8 to your computer and use it in GitHub Desktop.
JS vs Foi symbol count comparision: async IO
This file contains 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
// note: this requires a JS library for the IO monad, for example Monio: https://github.com/getify/monio/blob/master/MONIO.md#io-monad-one-monad-to-rule-them-all | |
// CHARACTER STATS... | |
// letters, numbers, string-literal-chars (a-z A-Z 0-9 / < >): 273 | |
// non-letters-non-whitespace: 78 | |
// simple symbols (. ` " ( ) { } $ , =): 63 | |
// compound symbols (=>): 4 (8 chars) | |
// optional semicolons: 6 | |
// whitespace: 73 | |
const getFavoriteMovies = userID => IO(function*(){ | |
const movieIDs = yield fetch(`/movies/${userID}`); | |
const movieData = yield Promise.all( | |
movieIDs.map(movieID => fetch(`/movie/${movieID}`)) | |
); | |
const itemsHTML = movieData.map(v => v.title) | |
.reduce( | |
(html, title) => `${html}<li>${title}</li>`, | |
"" | |
); | |
return setBodyHTML(`<ul>${itemsHTML}</ul>`); | |
}); | |
getFavoriteMovies(123).run(document); |
This file contains 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
// note: the .java on this filename is only for approx syntax highlighting on github | |
// CHARACTER STATS... | |
// letters, numbers, string-literal-chars (a-z A-Z 0-9 / < >): 231 | |
// non-letters-non-whitespace: 86 | |
// simple symbols (. " ` : ( ) { } , = ^ ~): 71 | |
// compound symbols (:: ~<<): 4 (9 chars) | |
// optional semicolons: 6 | |
// whitespace: 75 | |
defn getFavoriteMovies(userID) ^(IO ~<< { | |
def movieIDs:: fetch(`"/movies/`userID`"); | |
def movieData:: all( | |
movieIDs ~map (movieID) { | |
fetch(`"/movie/`movieID`") | |
} | |
); | |
def itemsHTML: (~fold)( | |
movieData ~map (.)|, "title"|, | |
"", | |
(html, title) { `"`html`<li>`title`</li>" } | |
); | |
::setBodyHTML(`"<ul>`itemsHTML`</ul>"); | |
}); | |
getFavoriteMovies(123).run(document); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you had never seen either JS or Foi before... and you were just making notes at a glance of what "syntax" you're seeing that you might not understand...
For JS:
=>
hmmm what is that? Oh, it's mathematical syntax for functions. umm, ok.function*
what the heck is that*
for? generators? umm, sure. why?yield
all about? oh, ok, generators. great.`
around the strings? oh, cool, a third whole way of delimiting strings. sure.${ }
? oh, that's special for those`
strings. gotcha, yeah.For Foi:
^
for? oh, returning values. ok.~<<
wtf? oh, ok, whatever "do syntax" is about. right.::
huh? ok, monadic chaining, blah blah.`
more of these special strings... but at least you keep using`
to delimit, instead of${ }
. sure.~map
? is that just like.map
? ok, cool, sure.(.)
wtf? umm, operators as functions, ok, that sounds useful I guess.| .. |
!? umm, partial application, that sounds fancy. sure.Does Foi have more syntax? Yes. But not a lot more. JS definitely has its syntax noise to sift through.
Moreover, Foi is also exposing a lot more functionality to you via its syntax, as opposed to having to use external libraries to accomplish similar tasks in JS.