<commit_before>// Task
// A Monad for asynchronous operations that potentially have side effects.

"use strict"

// Imports.
const mixin = require("./mixin.js")

// data Task e a = Task ((e -> ()) -> (a -> ()) -> ())
// ${doc.Task.Task}
function Task(fork) {
  if (!(this instanceof Task)) return new Task(fork)

  this.fork = fork
}

// bind :: (a -> Task e b) -> Task e a -> Task e b
// ${doc.Task.bind}
Task.prototype.bind = function(f) {
  const m = this

  return Task((rej, res) => m.fork(rej, x => f(x).fork(rej, res)))
}

// map :: (a -> b) -> Task e a -> Task e b
Task.prototype.map = function(f) {
  const m = this

  return Task((rej, res) => m.fork(rej, x => res(f(x))))
}

// of :: a -> Task e a
// ${doc.Task.of}
const of = x => Task((_, res) => res(x))

// sequence :: [Task e a] -> Task e [a]
const sequence = ms =>
  ms.reduce((r, m) => r.bind(t => m.map(x => t.concat([x]))), Task.of([]))

const doM = steps => Task((err, res) => {
  const gen = steps()
  const step = value => {
    const result = gen.next(value)
    if (result.done) {
      return result.value
    }

    return result.value.bind(step)
  }

  step().fork(err, res)
})

const toPromise = task => new Promise((res, rej) => task.fork(rej, res))

// Exports.
module.exports = mixin({
  doM,
  of,
  sequence,
  toPromise
})(Task)<commit_msg>Added fromPromise<commit_after>// Task
// A Monad for asynchronous operations that potentially have side effects.

"use strict"

// Imports.
const mixin = require("./mixin.js")

// data Task e a = Task ((e -> ()) -> (a -> ()) -> ())
// ${doc.Task.Task}
function Task(fork) {
  if (!(this instanceof Task)) return new Task(fork)

  this.fork = fork
}

// bind :: (a -> Task e b) -> Task e a -> Task e b
// ${doc.Task.bind}
Task.prototype.bind = function(f) {
  const m = this

  return Task((rej, res) => m.fork(rej, x => f(x).fork(rej, res)))
}

// map :: (a -> b) -> Task e a -> Task e b
Task.prototype.map = function(f) {
  const m = this

  return Task((rej, res) => m.fork(rej, x => res(f(x))))
}

// of :: a -> Task e a
// ${doc.Task.of}
const of = x => Task((_, res) => res(x))

// sequence :: [Task e a] -> Task e [a]
const sequence = ms =>
  ms.reduce((r, m) => r.bind(t => m.map(x => t.concat([x]))), Task.of([]))

const doM = steps => Task((err, res) => {
  const gen = steps()
  const step = value => {
    const result = gen.next(value)
    if (result.done) {
      return result.value
    }

    return result.value.bind(step)
  }

  step().fork(err, res)
})

const toPromise = task => new Promise((res, rej) => task.fork(rej, res))

const fromPromise = promise => Task((rej, res) => promise.then(res, rej))


// Exports.
module.exports = mixin({
  doM,
  fromPromise,
  of,
  sequence,
  toPromise
})(Task)
{'fraction_non_alphanumeric': 0.21092896174863388, 'fraction_numerical': 0.0, 'mean_word_length': 2.7823691460055096, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1716074', 'n_tokens_mistral': 1054, 'n_tokens_neox': 1019, 'n_words': 302}