Skip to content

Commit

Permalink
Update for head that now uses the new cloneIterable function (#353)
Browse files Browse the repository at this point in the history
  • Loading branch information
dalefrancis88 authored Dec 7, 2018
1 parent 46f57ec commit a2c0636
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/pointfree/head.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
/** @license ISC License (c) copyright 2016 original and current authors */
/** @author Ian Hofmann-Hicks (evil) */

const isArray = require('../core/isArray')
const isFunction = require('../core/isFunction')
const isIterable = require('../core/isIterable')
const isString = require('../core/isString')
const cloneIterable = require('../core/cloneIterable')

const { Nothing, Just } = require('../core/Maybe')

Expand All @@ -11,8 +14,13 @@ function head(m) {
return m.head()
}

if(isArray(m) || isString(m)) {
return !m.length ? Nothing() : Just(m[0])
}

if(isIterable(m)) {
const iterator = m[Symbol.iterator]()
const cloned = cloneIterable(m)
const iterator = cloned[Symbol.iterator]()
const head = iterator.next()

return head.done ? Nothing() : Just(head.value)
Expand Down

0 comments on commit a2c0636

Please sign in to comment.