idioms

Open and closed union types in Ocaml

自作多情 提交于 2019-12-03 11:56:28
问题 I'm looking into OCaml for the first time, having a bit of background with F# and Haskell. As such, a lot is familiar-looking, but one thing that isn't is the concept of "open" and "closed" unions (with the backtick and [< syntax). What are these useful for and how often are they used? 回答1: gasche's answer has good advice. I'm going to explain open and closed unions a bit more. First, you need to distinguish the two kinds of unions: basic variants (no backtick) and polymorphic variants (with

How does the “#map(&proc)” idiom work when introspecting module classes?

*爱你&永不变心* 提交于 2019-12-03 10:48:47
Presenting the Idiom I found an interesting but unexplained alternative to an accepted answer. The code clearly works in the REPL. For example: module Foo class Bar def baz end end end Foo.constants.map(&Foo.method(:const_get)).grep(Class) => [Foo::Bar] However, I don't fully understand the idiom in use here. In particular, I don't understand the use of &Foo , which seems to be some sort of closure, or how this specific invocation of #grep operates on the result. Parsing the Idiom So far, I've been able to parse bits and pieces of this, but I'm not really seeing how it all fits together. Here

“GetOrCreate” - does that idiom have an established name?

痞子三分冷 提交于 2019-12-03 10:06:22
Ok, consider this common idiom that most of us have used many times (I assume): class FooBarDictionary { private Dictionary<String, FooBar> fooBars; ... FooBar GetOrCreate(String key) { FooBar fooBar; if (!fooBars.TryGetValue(key, out fooBar)) { fooBar = new FooBar(); fooBars.Add(key, fooBar); } return fooBar; } } Does it have any kind of established name? (Yes, it's written in C#, but it can be "easily" transferred to C++. Hence that tag.) Lazy Loading http://en.wikipedia.org/wiki/Lazy_loading I always call such functions obtainSomething() . It sort of depends why you're doing it - the idiom

scheme for object-oriented programmers

佐手、 提交于 2019-12-03 08:26:21
问题 I'm thoroughly intrigued by Scheme, and have started with some toy programming examples, and am reading through Paul Graham's On Lisp. One thing I haven't been able to find is a book or website intended to teach Scheme to "OO people", i.e. people like myself who've done 99 % of their coding in c++/Java/Python. I see that closures are sort of object-y, in the sense that they have local state, and offer one or more functions that have access to that state. But I don't want to learn Scheme only

How to iterate through hash items, in an R environment?

拈花ヽ惹草 提交于 2019-12-03 07:42:19
问题 I'm trying to find a way to use a hash map in R, and after some searching I get the R-environment. But how can I iterate through all the items in an environment ? When I run the following code, I was expecting output like this : 1 2 But I get two lines of NULL instead, how can I get what I want ? map <- new.env(hash=T, parent=emptyenv()) assign('a', 1, map) assign('b', 2, map) for (v in ls(map)) { print(map$v) } 回答1: The use of "$" inside a function where it is desired to interpret the input

Idiomatic way of handling nullable or empty List in Kotlin

大憨熊 提交于 2019-12-03 04:50:32
问题 Say I have a variable activities of type List<Any>? . If the list is not null and not empty, I want to do something, otherwise I want to do something else. I came up with following solution: when { activities != null && !activities.empty -> doSomething else -> doSomethingElse } Is there a more idiomatic way to do this in Kotlin? 回答1: For some simple actions you can use the safe call operator, assuming the action also respects not operating on an empty list (to handle your case of both null

Open and closed union types in Ocaml

风格不统一 提交于 2019-12-03 02:32:47
I'm looking into OCaml for the first time, having a bit of background with F# and Haskell. As such, a lot is familiar-looking, but one thing that isn't is the concept of "open" and "closed" unions (with the backtick and [< syntax). What are these useful for and how often are they used? Gilles 'SO- stop being evil' gasche's answer has good advice . I'm going to explain open and closed unions a bit more. First, you need to distinguish the two kinds of unions: basic variants (no backtick) and polymorphic variants (with backtick). Basic variants are generative: if you define two types with the

How to iterate through hash items, in an R environment?

岁酱吖の 提交于 2019-12-02 21:11:44
I'm trying to find a way to use a hash map in R, and after some searching I get the R-environment. But how can I iterate through all the items in an environment ? When I run the following code, I was expecting output like this : 1 2 But I get two lines of NULL instead, how can I get what I want ? map <- new.env(hash=T, parent=emptyenv()) assign('a', 1, map) assign('b', 2, map) for (v in ls(map)) { print(map$v) } The use of "$" inside a function where it is desired to interpret the input is a common source of programming error. Use instead the form object[[value]] (without the quotes.) for (v

PHP - best way to initialize an object with a large number of parameters and default values

强颜欢笑 提交于 2019-12-02 17:06:44
I'm designing a class that defines a highly complex object with a ton (50+) of mostly optional parameters, many of which would have defaults (eg: $type = 'foo'; $width = '300'; $interactive = false; ). I'm trying to determine the best way to set up the constructor and instance/class variables in order to be able to: make it easy to use the class make it easy to auto-document the class (ie: using phpDocumentor) code this elegantly In light of the above, I don't want to be passing the constructor a ton of arguments. I will be passing it a single hash which contains the initialization values, eg:

What are the important language features (idioms) of Python to learn early on [duplicate]

倖福魔咒の 提交于 2019-12-02 15:44:47
This question already has an answer here: The Zen of Python [closed] 22 answers Python: Am I missing something? [closed] 16 answers I would be interested in knowing what the StackOverflow community thinks are the important language features (idioms) of Python. Features that would define a programmer as Pythonic. Python (pythonic) idiom - "code expression" that is natural or characteristic to the language Python. Plus, Which idioms should all Python programmers learn early on? Thanks in advance Related: Code Like a Pythonista: Idiomatic Python Python: Am I missing something? gahooa Python is a