From a2c0636f0a37b9d30c3dc213c162f345373c38e8 Mon Sep 17 00:00:00 2001 From: Dale Francis Date: Fri, 7 Dec 2018 12:30:24 -0800 Subject: [PATCH] Update for head that now uses the new cloneIterable function (#353) --- src/pointfree/head.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/pointfree/head.js b/src/pointfree/head.js index c00d0bdb5..d0b244d37 100644 --- a/src/pointfree/head.js +++ b/src/pointfree/head.js @@ -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') @@ -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)