क्या आपका प्रश्न जावास्क्रिप्ट में लेंस का उपयोग करने के बारे में है? यदि ऐसा है, तो मैं मदद करने में सक्षम हो सकता है। क्या आपने Ramda.js library की जांच की है? यह कार्यात्मक जेएस लिखने का एक शानदार तरीका है। के अपने दुश्मन मॉडल को देखकर शुरू करते हैं:
view(healthLens)(enemyModel); // 10
set(healthLens, 15)(enemyModel); // changes health from 10 to 15
over(healthLens, fireDamage)(enemyModel); // reduces enemyModel's health property by 10
जब से तुम एक दुश्मन के स्वास्थ्य के लिए fireDamage(..)
समारोह लागू कर रहे हैं, तो आप over(..)
का उपयोग करना चाहेंगे। इसके अलावा, चूंकि आपकी स्थिति निर्देशांक दुश्मन मॉडल में घिरे हुए हैं, इसलिए आप उन लोगों तक पहुंचने के लिए एक लेंस का उपयोग करना चाहेंगे। चलिए एक बनाते हैं और isInRange(..)
रिफैक्टर करते हैं जबकि हम इसमें हैं।
/* -- lenses -- */
const xLens = lensPath(['pos', 'x']);
const yLens = lensPath(['pos', 'y']);
const ptLens = lens(prop('pos'), assoc('pos'));
// since idk where 'radius' is coming from I'll hard-code it
let radius = 12;
const filterInRange = rad => filter(
over(ptLens, isInRange(rad)) // using 'ptLens' bc isInRange(..) takes 'radius' and a 'point'
);
const mapFireDamage = map(
over(healthLens, fireDamage) // using 'healthLens' bc fireDamage(..) takes 'health'
);
let newEnemies = compose(
mapFireDamage,
filterInRange(radius)
)(enemies);
: /* -- helper functions -- */
const square = x => x * x;
const gteRadSquared = radius => flip(gte)(square(radius));
let sumPointSquared = point => converge(
add,
[compose(square, prop('x')),
compose(square, prop('y'))]
)(point);
sumPointSquared = curry(sumPointSquared); // allows for "partial application" of fn arguments
/* -- refactored fn -- */
let isInRange = (radius, point) => compose(
gteRadSquared(radius),
sumPointSquared
)(point);
isInRange = curry(isInRange);
यहाँ है कि जब enemyModels का एक संग्रह के साथ काम कर कैसा लगेगा क्या करना है:
// NOTE: not sure if this works as you intended it to...
function isInRange(radius, point) {
return point.x^2 + point.y^2 >= radius^2; // maybe try Math.pow(..)
}
यहाँ एक कार्यात्मक दृष्टिकोण है:
एक संदर्भ के रूप में, यहाँ मूल fn है
मुझे आशा है कि इससे यह पता चलता है कि उपयोगी लेंस कितने उपयोगी हो सकते हैं।हालांकि कई सहायक कार्य हैं, मुझे लगता है कि कोड का अंतिम टुकड़ा सुपर अर्थपूर्ण है!
आखिरकार, मैं इस उदाहरण को अधिक पढ़ने योग्य बनाने के लिए रामदा से इन कार्यों के साथ अपने दायरे में बाढ़ कर रहा हूं। मैं इसे पूरा करने के लिए ES6 deconstruction का उपयोग कर रहा हूँ। यहां बताया गया है कि:
const {
add,
assocPath,
compose,
converge,
curry,
filter,
flip,
gte,
lens,
lensPath,
map,
over,
set,
path,
prop,
view
} = R;
// code goes below...
जेएसबीन में इसे आज़माएं! वे रामदा समर्थन प्रदान करते हैं।
@DustinGetz तो क्या आपका मतलब है कि आप इसे जावास्क्रिप्ट में अनुवाद करना चाहते हैं? –
@DustinGetz क्या आप इसे 'लेंस' लाइब्रेरी (यानी कार्यों के कार्यों) की शैली में बिल्कुल करने की कोशिश कर रहे हैं या क्या आप केवल प्रथम श्रेणी के गेटर्स और सेटर्स बना रहे हैं? –
मेरे मूल्य अधिक गहराई से घोंसले हैं, लेकिन ग्राफ नहीं। 4 की गहराई कहो। मुझे लगता है कि कार्यों के कार्यों की आवश्यकता है। –