Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

91
Visualizações
How to execute a function that returns a function inside of IO monad

I am using "Ramda/Fantasy" library for creating Monads. And i am trying to combine IO's to get element from the DOM and change its text content. But the problem is that function that changes content of the element needs to take two arguments element, and text that we want to set. I don't know how I can that efficiently I found one solution that looks like this:

const getElement = id => IO(() => document.querySelector(id));
const setElementText = element => text => IO(() => element.textcontent = text);

getElement('h1').map(setElementText).runIO()('New Title').runIO();

Is there a better way?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Two small changes to help with your example:

  • Swapping the order of the arguments to setElementText allows you to partially apply the text value.
  • Using chain rather than map remove the need for the second runIO call.
const getElement = id => IO(() => document.querySelector(id))
const setElementText = text => element => IO(() => element.textContent = text)

getElement('h1').chain(setElementText('New Title')).runIO()
about 4 years ago · Juan Pablo Isaza Relatório

0

I also found a second solution to use "ap()" method which applies the function (resulting from the action in this IO instance) to the value resulting from the action in the given IO instance.

import {IO} from "ramda-fantasy";

const getElement = id => IO(() => document.querySelector(id))
const setElementText = element => text => IO(() => element.textContent = text)

const someText = IO(() => 'New Title')

const setHeading = getElement('h1').map(setElementText).ap(someText).runIO();
setHeading.runIO(); // It works
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda