2017-01-02 1 views
4

私の現在の実装である以下のコードに仕様が記述された関数を記述したいと思います。それは動作します。しかし、私はしばらくの間、ラムダの機能を構成するものとして、それを無意味で書くことを試みてきましたが、解決策を見つけることができませんでした。この問題はobj => map(key => recordSpec[key](obj[key])にリンクされています。私は、すべてをポイントフリーに書くことができないように減らすことはできません。ラムダの型チェックヘルパー

どうすればいいですか?例えば

/** * check that an object : * - does not have any extra properties than the expected ones (strictness) * - that its properties follow the defined specs * Note that if a property is optional, the spec must include that case * @param {Object.<String, Predicate>} recordSpec * @returns {Predicate} * @throws when recordSpec is not an object */ function isStrictRecordOf(recordSpec) { return allPass([ // 1. no extra properties, i.e. all properties in obj are in recordSpec // return true if recordSpec.keys - obj.keys is empty pipe(keys, flip(difference)(keys(recordSpec)), isEmpty), // 2. the properties in recordSpec all pass their corresponding predicate // For each key, execute the corresponding predicate in recordSpec on the // corresponding value in obj pipe(obj => map(key => recordSpec[key](obj[key]), keys(recordSpec)), all(identity)), ] ) }

、あなたのrecordSpecのような仕様のオブジェクトを取得して、対応するキーの値と、各述語が適用され、これはR.whereを使用することです達成する isStrictRecordOf({a : isNumber, b : isString})({a:1, b:'2'}) -> true isStrictRecordOf({a : isNumber, b : isString})({a:1, b:'2', c:3}) -> false isStrictRecordOf({a : isNumber, b : isString})({a:1, b:2}) -> false

答えて

2

一つの方法、 2番目のオブジェクト。私は予想より短かったこと、すごい

const isStrictRecordOf = recordSpec => allPass([ 
    pipe(keys, flip(difference)(keys(recordSpec)), isEmpty), 
    where(recordSpec) 
]) 
+0

あなたの機能は、次のようになります。どうもありがとう。数多くのラムダ関数を得るのは難しいです。それでも私は、「どこで」期待していなかったのか、この点を自由に書いていく方法を知りました。しかし、とにかく、私は答えをテストし、テストが合格すればそれを受け入れるようにしています – user3743222

関連する問題