Skip to content

Instantly share code, notes, and snippets.

@mpj
Last active September 14, 2022 06:42
Show Gist options
  • Save mpj/ca167aad371c67372f3b to your computer and use it in GitHub Desktop.
Save mpj/ca167aad371c67372f3b to your computer and use it in GitHub Desktop.
This is the code from Monads - episode #21 of FunFunFunction (https://www.youtube.com/playlist?list=PL0zVEGEvSaeFSwPn06GKArptSxiP1Gff8)
const fetch = require('node-fetch')
const Bacon = require('baconjs')
function getInPortuguese(word) {
// Google Translate API is a paid (but dirt cheap) service. This is my key
// and will be disabled by the time the video is out. To generate your own,
// go here: https://cloud.google.com/translate/v2/getting_started
const apiKey =
'AIzaSyB4DyRHIsNhogQXmH16YKbZfR-lTXrQpq0'
const url =
'https://www.googleapis.com' +
'/language/translate/v2' +
'?key=' + apiKey +
'&source=en' +
'&target=pt' +
'&q=' + encodeURIComponent(word)
const promise = fetch(url)
.then(response => response.json())
.then(parsedResponse =>
parsedResponse
.data
.translations[0]
.translatedText
)
const stream = Bacon.fromPromise(promise)
return stream
}
const stream = new Bacon.Bus()
stream
.flatMap(word => getInPortuguese(word))
.map(word => word.toUpperCase())
.onValue(word => console.log(word))
stream.push('cat')
stream.push('meal')
stream.push('trumpet')
// Output of running this file will be:
// TROMBETA
// REFEIÇÃO
// GATO
@ryanpcmcquen
Copy link

@ahlusar1989, how about this?

https://babeljs.io/repl/#?evaluate=true&presets=es2015%2Creact%2Cstage-2&code=const%20fetch%20%3D%20require('node-fetch')%0Aconst%20Bacon%20%3D%20require('baconjs')%0A%0Afunction%20getInPortuguese(word)%20%7B%0A%20%20%2F%2F%20Google%20Translate%20API%20is%20a%20paid%20(but%20dirt%20cheap)%20service.%20This%20is%20my%20key%0A%20%20%2F%2F%20and%20will%20be%20disabled%20by%20the%20time%20the%20video%20is%20out.%20To%20generate%20your%20own%2C%0A%20%20%2F%2F%20go%20here%3A%20https%3A%2F%2Fcloud.google.com%2Ftranslate%2Fv2%2Fgetting_started%0A%20%20const%20apiKey%20%3D%0A%20%20%20%20'AIzaSyB4DyRHIsNhogQXmH16YKbZfR-lTXrQpq0'%0A%09const%20url%20%3D%0A%20%20%20%20'https%3A%2F%2Fwww.googleapis.com'%20%2B%0A%20%20%20%20'%2Flanguage%2Ftranslate%2Fv2'%20%2B%0A%20%20%09'%3Fkey%3D'%20%2B%20apiKey%20%2B%0A%20%20%20%20'%26source%3Den'%20%2B%0A%20%20%20%20'%26target%3Dpt'%20%2B%0A%20%20%20%20'%26q%3D'%20%2B%20encodeURIComponent(word)%0A%20%20const%20promise%20%3D%20fetch(url)%0A%20%09%09.then(response%20%3D%3E%20response.json())%0A%09%20%20.then(parsedResponse%20%3D%3E%0A%20%20%20%20%09parsedResponse%0A%20%20%20%20%20%20%09.data%0A%20%20%20%20%20%20%20%20.translations%5B0%5D%0A%20%20%20%20%20%20%20%20.translatedText%0A%20%20%20%20)%0A%20%20const%20stream%20%3D%20Bacon.fromPromise(promise)%0A%20%20return%20stream%0A%7D%0A%0Aconst%20stream%20%3D%20new%20Bacon.Bus()%0A%0Astream%0A%20%20.flatMap(word%20%3D%3E%20getInPortuguese(word))%0A%20%20.map(word%20%3D%3E%20word.toUpperCase())%0A%20%20.onValue(word%20%3D%3E%20console.log(word))%0A%0Astream.push('cat')%0Astream.push('meal')%0Astream.push('trumpet')%0A%0A%2F%2F%20Output%20of%20running%20this%20file%20will%20be%3A%0A%2F%2F%20TROMBETA%0A%2F%2F%20REFEI%C3%87%C3%83O%0A%2F%2F%20GATO

@mpj
Copy link
Author

mpj commented Aug 13, 2016

@ahlusar1989 yes, chain is another word often used for the flatMap operation. In Haskell it's called bind. Many different names for the same thing.

@Em-Ant
Copy link

Em-Ant commented Jan 1, 2017

Why don't you use simply

...
stream
  .flatMap(getInPortuguese)
  .map(word => word.toUpperCase())
  .onValue(console.log)
...

?

@elias551
Copy link

elias551 commented Jul 4, 2017

@Em-Ant Why not use this ? :)

...
stream
  .flatMap(getInPortuguese)
  .map(String.prototype.toUpperCase.apply)
  .onValue(console.log)
...

@palashmon
Copy link

palashmon commented Sep 22, 2017

For learning purpose, I am using this code, without the need of setting up Google Translate API 😉

const portuguese = ['que', 'bonito', 'rir', 'pequeno', 'longe'];
const randomWord = arr => arr[Math.floor(Math.random() * (arr.length - 1))];
const getPortuguese = word => {
    const promise = Promise.resolve(`${word} ${randomWord(portuguese)}`);
    const stream = Bacon.fromPromise(promise);
    return stream;
};

Output using map():

Bacon.fromPromise({})
Bacon.fromPromise({})
Bacon.fromPromise({})

Output using flatMap():

CAT RIR
MEAL QUE
TRUMPET BONITO

FunFunFunction!

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