2016-10-13 7 views
0

나는 "아마 bmDup은 함수가 아니다"라는 오류를 알기는하지만 정보가 아마도 어쩌면 내가 검색 할 필요가 있다고 알려주는지 이해하지 못한다.
함수에 랩핑하면 반환 값이 정의되지 않습니다.모나드 바인딩 방법

function Maybe() { 
 
    Object.freeze(this); 
 
}; 
 

 
function Just (x) { 
 
    this.toString = function() { return "Just " + x.toString(); }; 
 
    this.just = x; 
 
    Object.freeze(this); 
 
}; 
 
Just.prototype = new Maybe(); 
 
Just.prototype.constructor = Just; 
 

 
function Nothing() { 
 
    this.toString = function() { return "Nothing"; }; 
 
    Object.freeze(this); 
 
}; 
 
Nothing.prototype = new Maybe(); 
 
Nothing.prototype.constructor = Nothing; 
 

 
Maybe.unit = function (x) { 
 
    // return a Maybe that holds x 
 
    return new Just (x); 
 
}; 
 

 
Maybe.bind = function (f) { 
 
    // given a function from a value to a Maybe return a function from a Maybe to a Maybe 
 
    return new Maybe(f(this.just)); 
 
}; 
 

 
//to test 
 
function mDup(str) { 
 
    return new Just(str+str); 
 
} 
 
console.log(mDup("abc"));   // => new Just("abcabc") OK 
 
var bmDup = Maybe.bind(mDup); 
 
console.log(bmDup(new Just("abc"))) // => new Just("abcabc") NOK

+0

엄격한 모드를 시도해야합니다. 따라서 이러한 모든 실수는 예외로 나타납니다. – Bergi

답변

1

첫째, Maybe.bind이 실제로 무엇을하는지 살펴 보자.

Maybe.bind = function (f) { 
    // Construct a new Maybe object, passing in the result of f(this.just) 
    return new Maybe(f(this.just)); 
}; 

좋아, 그럼 Maybe 개체는 무엇입니까?

function Maybe() { 
    Object.freeze(this); 
}; 

글쎄, 고정 된 개체입니다. 따라서 Maybe.bind은 함수가 아닌 객체를 반환합니다.

+0

지침에 따르면 Maybe를 반환해야한다고 나와 있는데, 이는 기능이 아닙니다. 그래서 나는 이것이 "가치에서 기능을 얻은 것"이 무엇을 의미하는지 이해하지 못할 수도 있습니다. – kalia

+0

@kalia 당신은 나를 잡았습니다. 내가 모나드를 이해할 때마다, 나는 정말로 그렇지 않다는 것을 깨닫는다. –