idioms

What is the clojure equivalent of the Python idiom “if __name__ == '__main__'”?

半城伤御伤魂 提交于 2019-11-26 17:41:15
问题 I'm dabbling in clojure and am having a little trouble trying to determine the clojure (and / or Lisp) equivalent of this common python idiom. The idiom is that at the bottom of a python module there is often a bit of test code, and then a statement which runs the code, for example: # mymodule.py class MyClass(object): """Main logic / code for the library lives here""" pass def _runTests(): # Code which tests various aspects of MyClass... mc = MyClass() # etc... assert 2 + 2 == 4 if __name__

Idiom(s) for “for each except the last” (or “between each consecutive pair of elements”) [duplicate]

人盡茶涼 提交于 2019-11-26 17:37:37
问题 This question already has answers here : Printing lists with commas C++ (26 answers) How can I check if I'm on the last element when iterating using foreach syntax [duplicate] (6 answers) Closed 3 years ago . Everyone encounters this issue at some point: for(const auto& item : items) { cout << item << separator; } ... and you get an extra separator you don't want at the end. Sometime it's not printing, but, say, performing some other action, but such that consecutive actions of the same type

Pairs from single list

為{幸葍}努か 提交于 2019-11-26 17:22:07
Often enough, I've found the need to process a list by pairs. I was wondering which would be the pythonic and efficient way to do it, and found this on Google: pairs = zip(t[::2], t[1::2]) I thought that was pythonic enough, but after a recent discussion involving idioms versus efficiency , I decided to do some tests: import time from itertools import islice, izip def pairs_1(t): return zip(t[::2], t[1::2]) def pairs_2(t): return izip(t[::2], t[1::2]) def pairs_3(t): return izip(islice(t,None,None,2), islice(t,1,None,2)) A = range(10000) B = xrange(len(A)) def pairs_4(t): # ignore value of t!

Named Parameter idiom in Java

我与影子孤独终老i 提交于 2019-11-26 17:08:33
How to implement Named Parameter idiom in Java? (especially for constructors) I am looking for an Objective-C like syntax and not like the one used in JavaBeans. A small code example would be fine. Thanks. The best Java idiom I've seem for simulating keyword arguments in constructors is the Builder pattern, described in Effective Java 2nd Edition . The basic idea is to have a Builder class that has setters (but usually not getters) for the different constructor parameters. There's also a build() method. The Builder class is often a (static) nested class of the class that it's used to build.

How to name this key-oriented access-protection pattern?

感情迁移 提交于 2019-11-26 16:34:18
问题 Apparently this key-oriented access-protection pattern: class SomeKey { friend class Foo; SomeKey() {} // possibly non-copyable too }; class Bar { public: void protectedMethod(SomeKey); // only friends of SomeKey have access }; ... doesn't have a known name yet, thus i'd like to find a good one for it so we can refer to it without breaking our tongues. Suggestions? It should be: succinct convey the intent of access-protection ideally imply that no proxying is required (?) 回答1: I like, in

How to automatically register a class on creation

≡放荡痞女 提交于 2019-11-26 12:25:22
问题 I was wondering whether a design pattern or idiom exists to automatically register a class type. Or simpler, can I force a method to get called on a class by simply extending a base class? For example, say I have a base class Animal and extending classes Tiger and Dog , and I have a helper function that prints out all classes that extend Animal . So I could have something like: struct AnimalManager { static std::vector<std::string> names; static void registerAnimal(std::string name) { //if

Most idiomatic way to print a time difference in Java?

本小妞迷上赌 提交于 2019-11-26 12:18:01
问题 I\'m familiar with printing time difference in milliseconds: long time = System.currentTimeMillis(); //do something that takes some time... long completedIn = System.currentTimeMillis() - time; But, is there a nice way print a complete time in a specified format (eg: HH:MM:SS) either using Apache Commons or even the dreaded platform API\'s Date/Time objects? In other words, what is the shortest, simplest, no nonsense way to write a time format derived from milliseconds in Java? 回答1: Apache

Get the key corresponding to the minimum value within a dictionary

别等时光非礼了梦想. 提交于 2019-11-26 11:58:54
If I have a Python dictionary, how do I get the key to the entry which contains the minimum value? I was thinking about something to do with the min() function... Given the input: {320:1, 321:0, 322:3} It would return 321 . Best: min(d, key=d.get) -- no reason to interpose a useless lambda indirection layer or extract items or keys! Mark Rushakoff Here's an answer that actually gives the solution the OP asked for: >>> d = {320:1, 321:0, 322:3} >>> d.items() [(320, 1), (321, 0), (322, 3)] >>> # find the minimum by comparing the second element of each tuple >>> min(d.items(), key=lambda x: x[1])

Python nested looping Idiom

老子叫甜甜 提交于 2019-11-26 11:13:14
问题 I often find myself doing this: for x in range(x_size): for y in range(y_size): for z in range(z_size): pass # do something here Is there a more concise way to do this in Python? I am thinking of something along the lines of for x, z, y in ... ? : 回答1: You can use itertools.product: >>> for x,y,z in itertools.product(range(2), range(2), range(3)): ... print x,y,z ... 0 0 0 0 0 1 0 0 2 0 1 0 0 1 1 0 1 2 1 0 0 1 0 1 1 0 2 1 1 0 1 1 1 1 1 2 回答2: If you've got numpy as a dependency already, numpy

DRY Ruby Initialization with Hash Argument

孤街浪徒 提交于 2019-11-26 10:11:06
问题 I find myself using hash arguments to constructors quite a bit, especially when writing DSLs for configuration or other bits of API that the end user will be exposed to. What I end up doing is something like the following: class Example PROPERTIES = [:name, :age] PROPERTIES.each { |p| attr_reader p } def initialize(args) PROPERTIES.each do |p| self.instance_variable_set \"@#{p}\", args[p] if not args[p].nil? end end end Is there no more idiomatic way to achieve this? The throw-away constant