I am trying to jquery each function with my style of functional programming. I have defined ramda library before like

const R = require('ramda')

The original each function is

 function each( obj, callback ) {
            var length, i = 0;
            if ( isArrayLike( obj ) ) {
                length = obj.length;
                for ( ; i < length; i++ ) {
                    if ( callback.call( obj[ i ], i, obj[ i ] ) === false ) {
                        break;
                    }
                }
            } else {
                for ( i in obj ) {
                    if ( callback.call( obj[ i ], i, obj[ i ] ) === false ) {
                        break;
                    }
                }
            }
            return obj;
        }

I tried to change my own function like

function my_each( obj, callback ) {
    var length, i = 0;
    let fun1 = 
    R.curry(obj =>{
        let length = obj.length;
        let p = R.range(0,length)
        if ( callback.call( obj[ p ], p, obj[ p ] ) === false ) {
            return obj;
        }
     })
    let fun2 = 
        R.curry(obj =>{  
        let key = R.keys(obj);
        if ( callback.call( obj[ key ], key, obj[ key ] ) === false ) 
        {return obj;}
        })    

    let main =
    R.ifElse(
        obj =>{isArrayLike(obj) == true},
        fun1,
        fun2
     )
    return R.map(main,callback);
 }

But it does not work. I think there are some type errors. I cannot guess where is it.

Expected output of

var arr= [ {title : 'a', url : 'hello'}, {title : 'n', url : 'bye'} ]; 
each(arr, function (index, item) {
var result = ''; result += index +' : ' + item.title + ', ' +item.url;
console.log(result);
}
0: a, hello    
1: n, bye

0 Answers