function-binding

Is JavaScript function.bind() supported in google sheets?

余生颓废 提交于 2021-02-11 08:04:33
问题 Here a simple code i'm trying to run in google sheets script. The purpose is supplying the foreach callback function additional parameters. function print(str, num) { Logger.log(str + ": " + num); } function test() { var array = [1,2,3]; var str = "Stam"; //This line has an exception // TypeError: Cannot convert null to an object array.forEach(print.bind(null, str)); } test(); this code is based on the solution described here. I know there are other solutions, though i want to understand why

Generalised Curry - Javascript

烈酒焚心 提交于 2020-01-14 19:05:27
问题 While reading an article on implementation of a generalised curry in Javascript, I stumbled upon this piece of code. function curry(fn) { return (...xs) => { if (xs.length === 0) { throw Error('EMPTY INVOCATION'); } if (xs.length >= fn.length) { return fn(...xs); } return curry(fn.bind(null, ...xs)); }; } I am unable to grok the part of the explanation which states We create a copy of fn that has the first k arguments bound (partially applied) and pass it to curry as the next fn, with its

Why can't I bind directly console.log on IE9 with developer tools open?

拜拜、爱过 提交于 2020-01-14 08:04:53
问题 With developer tools open in IE9, this code works : var log = Function.prototype.bind(console.log, console); But if I type console.log(console, console.log); var log = console.log.bind(console); then I get this : Why ? Is that a known IE bug or a normal behavior ? Does it affect other functions (I had no problem with window.alert which is also native) ? 回答1: As the related answer says, it is simply because the log function from the console object in IE doesn't inherit from Function . It's a

console.log() called on object other than console

我的未来我决定 提交于 2019-12-21 03:09:15
问题 I remember that always when I wanted to pass console.log as a callback parameter to some function, it didn't work unless I used the bind() method to bind console to it. For example: const callWithTest = callback => callback('test'); callWithTest(console.log); // That didn't use to work. callWithTest(console.log.bind(console)); // That worked (and works) fine. See Uncaught TypeError: Illegal invocation in javascript. However, recently I noticed that console.log() works fine even when called on

Binding / piping output of run() on/into function in python3 (lynux)

拜拜、爱过 提交于 2019-12-12 05:58:02
问题 I am trying to use output of external program run using the run function. this program regularly throws a row of data which i need to use in mine script I have found a subprocess library and used its run() / check_output() Example: def usual_process(): # some code here for i in subprocess.check_output(['foo','$$']): some_function(i) Now assuming that foo is already in a PATH variable and it outputs a string in semi-random periods. I want the program to do its own things, and run some_function

Java: Function with one varargs argument and function with same name and one argument of same type is allowed? [duplicate]

穿精又带淫゛_ 提交于 2019-12-11 05:58:41
问题 This question already has answers here : Why does type-promotion take precedence over varargs for overloaded methods (2 answers) Closed last year . While preparing for Java certification exam I was quite surprised to see that Java allows this: public class Consumer { public void buy(Object o) { System.out.println("Buying one object"); } public void buy(Object... o) { System.out.println("Buying multiple objects"); } public static void main(String[] args) { Consumer consumer = new Consumer();

How to binding a generic function in TypeScript

♀尐吖头ヾ 提交于 2019-12-11 04:09:52
问题 I'm trying to bind a generic function to a specific context and parameter, however I can't seem to be able to figure out the correct way to do it. Here's my code: interface A {} interface Repository<T> { save(item: T): boolean; } const updater = <T>(repo: Repository<T>, item: T) => { // do things and update }; const items: A[] = []; const repo: Repository<A> = { save(item: A) { /* do things */ return true; } } items.forEach(updater<A>.bind(null, repo)); // Line with issue The idea here would

Using bind for partial application without affecting the receiver

南笙酒味 提交于 2019-12-04 05:28:52
问题 If I want to partially apply a function I can use bind , but it seems I have to affect the receiver of the function (the first argument to bind ). Is this correct? I want to perform partial application using bind without affecting the receiver. myFunction.bind(iDontWantThis, arg1); // I dont want to affect the receiver 回答1: partial application using bind without affecting the receiver That's not possible. bind was explicitly designed to partially apply the "zeroth argument" - the this value,

console.log() called on object other than console

时间秒杀一切 提交于 2019-12-03 09:24:11
I remember that always when I wanted to pass console.log as a callback parameter to some function, it didn't work unless I used the bind() method to bind console to it. For example: const callWithTest = callback => callback('test'); callWithTest(console.log); // That didn't use to work. callWithTest(console.log.bind(console)); // That worked (and works) fine. See Uncaught TypeError: Illegal invocation in javascript . However, recently I noticed that console.log() works fine even when called on object other than console. For example: console.log.call(null, 'test'); logs 'test' . When and why

Using bind for partial application without affecting the receiver

强颜欢笑 提交于 2019-12-02 06:40:21
If I want to partially apply a function I can use bind , but it seems I have to affect the receiver of the function (the first argument to bind ). Is this correct? I want to perform partial application using bind without affecting the receiver. myFunction.bind(iDontWantThis, arg1); // I dont want to affect the receiver partial application using bind without affecting the receiver That's not possible. bind was explicitly designed to partially apply the "zeroth argument" - the this value, and optionally more arguments. If you only want to fix the first (and potentially more) parameters of your