So I thought I was crazy, and I may be but this seems pretty simple. Say I have this bit of code:
let a = {};
a.b.c.d.e.f.g = 'Something Awesome';
Now you can imagine the if-check nightmare that would have to take place to see if each level exists. And even if I had a fancy looping way to do it, someone else has had to think this up.
I think CoffeeScript had something like this, but I can't seem to find anything on it. I was also wondering if there was an ECMAScript6 or lodash way of doing the same type of thing?
Maybe something like:
set(a, b, c, d, e, f, g, 'Something Awesome');
And in that case even:
set(a, b, c, d, {e: {f: {g: 'Something Awesome'}}});
But the last option would wipe out anything that may exist in e, f, and g.
You are referring to lodash's _.set():
const a = {};
_.set(a, 'b.c.d.e.f.g', 'Something Awesome');
console.log(a);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
Related
Say I have a function that has (A, B, C, D, E) arguments.
Foo(A, B, C, D, E)
But I don't need them all the time. Sometimes I just need A and C for example. Right now I would have to call it like this:
Foo('beef', '', 'sour cream', '', '')
But those blanks irritate me. Is there a way to say to make it just be Foo('beef', 'sour cream')?
Maybe Foo(A='beef', C='sour cream')?
I've tried making them optional, but as I understand it, I can't expect the program to understand I want B blank. It expects 5 arguments, it needs 5.
You can name them like this:
function foo({A, B, C, D, E}) {
}
foo({A:3, D:5})
What we're technically doing here is passing an object, and using destructuring to extract the properties of the object into local variables.
If I want to define multiple symbols in a row, the syntax is a little verbose:
const a = Symbol('a'),
b = Symbol('b'),
c = Symbol('c');
I came up with a slightly more concise way to do it:
const [a, b, c] = ['a','b','c'].map(key => Symbol(key))
Is that the most concise way possible, or is there some dedicated syntax for declaring multiple symbols that I'm unaware of?
P.S. 'a' 'b' and 'c' are just arbitrary examples of course. I realize that one could get clever with this particular example ;)
There's a fun part of FP that you might be missing out on, which is that in a pure system, you can always replace an anonymous function with a named reference which is returned (and more generally, you can always replace a function with the value it would have returned), assuming that the function you're replacing and the function you're replacing it with take the same number of arguments.
const [a, b, c] = ["a", "b", "c"].map(Symbol);
Should work just fine, as long as you keep in mind that you can cause accidents here, by passing in functions that expect more than 1 argument.
Sure, not much more terse than what you already had. ...but still more than none.
It can be as concise as
const [a, b, c] = ['a','b','c'].map(Symbol);
There's no way to skip abc tautology here because variable names should be explicitly written, as well as symbol description.
Symbol descriptions are helpful for debugging. If they can be omitted (not recommended), it becomes
const [a, b, c] = [,,].map(Symbol);
for fixed amount of variables. And
const [a, b, c, d /*, ... */] = function* () { for(;;) yield Symbol() }();
for unlimited amount of variables.
One option, if you don't mind using ES6-only features that can't be polyfilled, is to use a Proxy that returns a symbol for each property access. You could make a helper module file like
module.exports = function autoSymbols() {
return new Proxy({}, {
get(target, name, receiver){
return Symbol(name);
},
});
}
then do
const {a, b, c} = require('./auto-symbols');
though personally I think enumerating the Symbol instances manually is more easily maintained.
So I found several implementations of the following type of easing functions:
function easeInQuad(t, b, c, d) {
return c*(t/=d)*t + b;
}
but would really like to use the following format instead:
function easeInQuad(pos) {
return Math.pow(pos, 2);
}
How to calculate "pos" ?
Thanks in advance.
EDIT:
They are indeed 2 different functions.
The first expects t: current time, b: beginning value, c: change in value, d: duration
The second expects some calculus to be made beforehand in order to do the same as the first.
I'm trying to find out what are those calculus.
EDIT 2:
I believe I haven't explain my self properly and apologise for that.
Let me put it this way:
How to use the following:
https://github.com/danro/easing-js/blob/master/easing.js
My problem is best explained with a simple code example:
function doThing(a, ...b, c) {
console.log(a, b, c)
}
doThing(1,2,3,4); // Expect "1 [2,3] 4"
This instead gives a syntax error Unexpected token, pointing to the comma after the b in the function definition.
Is it not possible to put a single parameter after a 'rested' parameter with ES6? If not, any idea why? It would be really useful for me. Thanks!
Edit: I initially thought that doThing(a, ...b, c) would be completely unambiguous, but I see now that doThing(1,2,3) would need an arbitrary rule to decide whether the 3 goes in b or c (i.e. if we pass a number of params that is less than or equal to the number of params in the function definition).
developer.mozilla.org says:
If the last named argument of a function is prefixed with ..., it becomes an array whose elements from 0 to theArgs.length are supplied by the actual arguments passed to the function.
Actually this feature of ES6 is called Rest parameters so it is meant to be the last in the list of parameters.
So this code will work:
function doThing(a, b, ...c) {
console.log(a, b, c);
}
doThing(1,2,3,4);
I mean this more exactly:
var MyClass = function(){
return {
init: function(a, b, c){
this.__a = a;
this.__b = b;
this.__c = c;
}
,doSomething: function(){
// when to use this:
this.__subThingType1();
// or this?
this.__subThingType2(this.__a, this.__b, this.__c);
}
,__subThingType1: function(){
var a = this.__a;
var b = this.__b;
var c = this.__c;
// do dirty things with a, b and c
}
,__subThingType2: function(a, b, c){
// do dirty things with a, b and c
}
}
When should I use the type1 or type2? I cannot figure out a rule for this, I find my code that there is a mix between the two, because sometimes I think I will remove that method from this class and put it in a lib.
But is not a good rule, because sometime I leave the method in the class, so a lot of mixing happens.
Also, the method should return a value or alter the instance variables?
Thanks
Store intrinsic data in the instance; pass in extrinsic data as arguments. For example, your class represents a circle. Store its center and radius as intrinsic data. But your method intersectionWith takes an argument anotherCircle and returns the area of intersection.
The difference between intrinsic and extrinsic is not always so obvious, but coding is not always obvious. Use your judgement.
More generally, if you're puzzled by this kind of thing, you probably want to learn more about the paradigm of Object-oriented Programming.