idiomatic

Clojure: iterate over map of sets

為{幸葍}努か 提交于 2019-12-23 17:21:35
问题 This is pretty much a follow-up to my last question (Clojure idiomatic way to update multiple values of map), but not quite the same. (keep in mind that I'm fairly new to Clojure and functional languages alike) suppose I have the following data structure, defined as a map of sets: (def m1 {:1 #{2} :2 #{1 3} :3 #{1}}) and a map of maps as such: (def m2 {:1 {:1 0 :2 12 :3 23} :2 {:1 23 :2 0 :3 4} :3 {:1 2 :2 4 :3 0}}) What I want to do is update the registries of m2 that have a correspondence

make-keyword-map in Clojure - Idiomatic?

∥☆過路亽.° 提交于 2019-12-23 10:14:10
问题 I have been writing some Clojure recently, and I found myself using the following pattern frequently enough: (let [x (bam) y (boom)] {:x x :y y}) So I went ahead and wrote the following macro: (defmacro make-keyword-map [& syms] `(hash-map ~@(mapcat (fn [s] [(keyword (name s)) s]) syms))) With that, code now looks like: (let [x (bam) y (boom)] (make-keyword-map x y) Would this sort of macro be considered idiomatic? Or am I doing something wrong, and missing some already established pattern to

Scala avoid using null

泄露秘密 提交于 2019-12-23 08:57:05
问题 I hava a project on github that is analysed by codacy . The analysis suggest to "Avoid using null" for the following line of code: def doSomethingWithPath(path:Path) = { require(path != null, "Path should not be null") //<-to avoid ... } What is the simplest scala idiomatic thing to do to fix it? 回答1: I would keep it as is. You aren't really using null here, just guarding against it. The alternative could be to just delete that line altogether, and decide to not handle the null at all.

When explicitly initializing std::optional's, should I use nullopt? [closed]

别等时光非礼了梦想. 提交于 2019-12-22 05:19:14
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 3 years ago . An std::optional<T> can be initialized to the disengaged state like so: std::optional<int> oi { nullopt }; but also like so: std::optional<int> oi { }; and similarly for assignment ( oi = {} or oi = nullopt ). Other than personal preference / sense of aesthetics, is there a

Idiomatic construction to check whether a collection is ordered

不打扰是莪最后的温柔 提交于 2019-12-18 10:13:47
问题 With the intention of learning and further to this question, I've remained curious of the idiomatic alternatives to explicit recursion for an algorithm that checks whether a list (or collection) is ordered. (I'm keeping things simple here by using an operator to compare and Int as type; I'd like to look at the algorithm before delving into the generics of it) The basic recursive version would be (by @Luigi Plinge): def isOrdered(l:List[Int]): Boolean = l match { case Nil => true case x :: Nil

HOWTO: Idiomatic Rust for callbacks with gtk (rust-gnome)

回眸只為那壹抹淺笑 提交于 2019-12-18 05:45:07
问题 I am currently learning Rust and looking to use it for developing a GUI based application with GTK+. My problem relates to registering callbacks to respond to GTK events/signals and mutating state within those callbacks. I have a working but inelegant solution, so I would like to ask if there is a cleaner, more idiomatic solution. I have implemented my code as a struct with method implementations, where the struct maintains references to the GTK widgets along with other state that it needs.

Should I be trying to create a reversible enum in Java or is there a better way?

你。 提交于 2019-12-13 12:00:19
问题 I seem to have faced this problem many times and I wanted to ask the community whether I am just barking up the wrong tree. Basically my question can be distilled down to this: if I have an enum (in Java) for which the values are important, should I be using an enum at all or is there a better way, and if I do use an enum then what is the best way to reverse the lookup? Here's an example. Suppose I want to create a bean representing a specific month and year. I might create something like the

Idiomatic Swift multiplex notifications?

北城以北 提交于 2019-12-13 01:50:42
问题 I'm defining a class structure that needs to notify its consumers when interesting things happen to it (gets new data, and so on). Similar to a delegate relationship, but there may be many consumers. And a more direct relationship than casting an NSNotification into the wind. My inclination in most Objective-C programs would just be to define a protocol, and let those consumers implement it and register themselves as id<MyProtocol> ; I'd stuff them into an NSMutableSet , which I'd iterate

Is it common practice to memset reallocated memory to 0?

佐手、 提交于 2019-12-12 17:23:33
问题 In a C book I found in an example for implementing a dynamically resizing array this code (simplified): void *contents = realloc(array->contents, array->max * sizeof(void *)); array->contents = contents; memset(array->contents + old_max, 0, array->expand_rate + 1); Source: Learn C The Hard Way – Chapter 34 I was a bit surprised what memset is supposed to achieve here, but then I understood it's used in order to "zero out" the reallocated memory. I googled in order to find out, if this is what

Is this usage of Option idiomatic in F#?

安稳与你 提交于 2019-12-12 10:34:02
问题 I have the following function that checks the existance of a customer in a data source and returns the id. Is this the right/idiomatic way of using the Option type? let findCustomerId fname lname email = let (==) (a:string) (b:string) = a.ToLower() = b.ToLower() let validFName name (cus:customer) = name == cus.firstname let validLName name (cus:customer) = name == cus.lastname let validEmail email (cus:customer) = email == cus.email let allCustomers = Data.Customers() let tryFind pred =