Skip to content

Instantly share code, notes, and snippets.

@getify
Last active December 4, 2023 20:31
Show Gist options
  • Save getify/3542996ab54b5be2a648ecfcb6bb6bc8 to your computer and use it in GitHub Desktop.
Save getify/3542996ab54b5be2a648ecfcb6bb6bc8 to your computer and use it in GitHub Desktop.
JS vs Foi symbol count comparision: async IO
// 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);
// 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);
@getify
Copy link
Author

getify commented Dec 4, 2023

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?
  • what is yield all about? oh, ok, generators. great.
  • what are these ` around the strings? oh, cool, a third whole way of delimiting strings. sure.
  • and ${ }? oh, that's special for those ` strings. gotcha, yeah.

For Foi:

  • what's this ^ for? oh, returning values. ok.
  • And ~<< 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.
  • what is this | .. |!? 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment