idioms

jquery .val() += idiom

放肆的年华 提交于 2019-11-30 01:48:59
问题 What's the clearest commonly used idiom for this jQuery snippet? $('#someTextarea').val( $('#someTextarea').val() + someString ); It feels clunky to wrap the original code in a one-line function EDIT: So I can pass a function, which is cool... but my real intentions are for jsfiddles, where I currently do stuff like this: function lazylog (str) { $('#ta').val( $('#ta').val() + str + '\n' ); } // or function lazylogPlain (str) { document.getElementById('ta').value += str + '\n'; } // view

Sentinel object and its applications?

為{幸葍}努か 提交于 2019-11-29 23:18:08
问题 I know in python the builtin object() returns a sentinel object. I'm curious to what it is, but mainly its applications. 回答1: object is the base class that all other classes inherit from in python 3. There's not a whole lot you can do with a plain old object. However an object's identity could be useful. For example the iter function takes a sentinel argument that signals when to stop termination. We could supply an object() to that. sentinel = object() def step(): inp = input('enter

Best Loop Idiom for special casing the last element

ⅰ亾dé卋堺 提交于 2019-11-29 20:15:56
I run into this case a lot of times when doing simple text processing and print statements where I am looping over a collection and I want to special case the last element (for example every normal element will be comma separated except for the last case). Is there some best practice idiom or elegant form that doesn't require duplicating code or shoving in an if, else in the loop. For example I have a list of strings that I want to print in a comma separated list. (the do while solution already assumes the list has 2 or more elements otherwise it'd be just as bad as the more correct for loop

String concatenation with Groovy

梦想的初衷 提交于 2019-11-29 19:30:14
What is the best (idiomatic) way to concatenate Strings in Groovy? Option 1: calculateAccountNumber(bank, branch, checkDigit, account) { bank + branch + checkDigit + account } Option 2: calculateAccountNumber(bank, branch, checkDigit, account) { "$bank$branch$checkDigit$account" } I've founded an interesting point about this topic in the old Groovy website: Things you can do but better leave undone. As in Java, you can concatenate Strings with the "+" symbol. But Java only needs that one of the two items of a "+" expression to be a String, no matter if it's in the first place or in the last

Preferred idiom for endianess-agnostic reads

[亡魂溺海] 提交于 2019-11-29 18:37:34
问题 In the Plan 9 source code I often find code like this to read serialised data from a buffer with a well-defined endianess: #include <stdint.h> uint32_t le32read(uint8_t buf[static 4]) { return (buf[0] | buf[1] << 8 | buf[2] << 16 | buf[3] << 24); } I expected both gcc and clang to compile this code into something as simple as this assembly on amd64: .global le32read .type le32read,@function le32read: mov (%rdi),%eax ret .size le32read,.-le32read But contrary to my expectations, neither gcc

Idiom for simulating run-time numeric template parameters?

元气小坏坏 提交于 2019-11-29 16:46:43
Suppose we have template <unsigned N> foo() { /* ... */ } defined. Now, I want to implement do_foo(unsigned n); which calls the corresponding variant of foo() . This is not merely a synthetic example - this does actually happen in real life (of course, not necessarily with void-to-void functions and just one template parameter, but I'm simplfying. Of course, in C++, we can't have the following: do_foo(unsigned n) { foo<n>(); } and what I do right now is do_foo(unsigned n) { switch(n) { case n_1: foo<n_1>(); break; case n_2: foo<n_2>(); break; /* ... */ case n_k: foo<n_k>(); break; } } when I

Is str.replace(..).replace(..) ad nauseam a standard idiom in Python?

跟風遠走 提交于 2019-11-29 13:07:27
For instance, say I wanted a function to escape a string for use in HTML (as in Django's escape filter ): def escape(string): """ Returns the given string with ampersands, quotes and angle brackets encoded. """ return string.replace('&', '&').replace('<', '<').replace('>', '>').replace("'", ''').replace('"', '"') This works, but it gets ugly quickly and appears to have poor algorithmic performance (in this example, the string is repeatedly traversed 5 times). What would be better is something like this: def escape(string): """ Returns the given string with ampersands, quotes and angle brackets

Multiple Exits From F# Function

*爱你&永不变心* 提交于 2019-11-29 12:24:38
问题 I could do this easily in C++ (note: I didn't test this for correctness--it's only to illustrate what I'm trying to do): const int BadParam = -1; const int Success = 0; int MyFunc(int param) { if(param < 0) { return BadParam; } //normal processing return Success; } But I cannot figure out how to exit a routine early in F#. What I want to do is to exit the function on a bad input but continue if the input is ok. Am I missing some fundamental property of F# or am I approaching the problem in

How to implement an interface class using the non-virtual interface idiom in C++?

倾然丶 夕夏残阳落幕 提交于 2019-11-29 11:35:58
In C++ an interface can be implemented by a class with all its methods pure virtual. Such a class could be part of a library to describe what methods an object should implement to be able to work with other classes in the library: class Lib::IFoo { public: virtual void method() = 0; }; : class Lib::Bar { public: void stuff( Lib::IFoo & ); }; Now I want to to use class Lib::Bar, so I have to implement the IFoo interface. For my purposes I need a whole of related classes so I would like to work with a base class that guarantees common behavior using the NVI idiom: class FooBase : public IFoo //

What is “sentry object” in C++?

南楼画角 提交于 2019-11-29 08:15:05
问题 I answered this question, and Potatoswatter answered too as The modern C++ equivalent would be a sentry object: construct it at the beginning of a function, with its constructor implementing call(), and upon return (or abnormal exit), its destructor implements I am not familiar with using sentry objects in C++. I thought they were limited to input and output streams. Could somebody explain to me about C++ sentry objects as well as how to use them as an around interceptor for one or more