let

Lazy Var vs Let

若如初见. 提交于 2019-11-30 10:42:36
I want to use Lazy initialization for some of my properties in Swift. My current code looks like this: lazy var fontSize : CGFloat = { if (someCase) { return CGFloat(30) } else { return CGFloat(17) } }() The thing is that once the fontSize is set it will NEVER change. So I wanted to do something like this: lazy let fontSize : CGFloat = { if (someCase) { return CGFloat(30) } else { return CGFloat(17) } }() Which is impossible. Only this works: let fontSize : CGFloat = { if (someCase) { return CGFloat(30) } else { return CGFloat(17) } }() So - I want a property that will be lazy loaded but will

Clojure's 'let' equivalent in Scala

社会主义新天地 提交于 2019-11-30 06:38:51
Often I face following situation: suppose I have these three functions def firstFn: Int = ... def secondFn(b: Int): Long = ... def thirdFn(x: Int, y: Long, z: Long): Long = ... and I also have calculate function. My first approach can look like this: def calculate(a: Long) = thirdFn(firstFn, secondFn(firstFn), secondFn(firstFn) + a) It looks beautiful and without any curly brackets - just one expression. But it's not optimal, so I end up with this code: def calculate(a: Long) = { val first = firstFn val second = secondFn(first) thirdFn(first, second, second + a) } Now it's several expressions

Why are parentheses needed on this F# function?

醉酒当歌 提交于 2019-11-30 03:32:28
问题 Why are parentheses needed on read_rest_of_csv below? let read_rest_of_csv() = csv_data.Add(csv_fileH.ReadFields()) |> ignore not csv_fileH.EndOfData while read_rest_of_csv() do ignore None Without the parentheses, the loop will not terminate. open System open System.Threading open System.Collections.Generic open System.Linq open System.Text open System.Threading.Tasks open System.IO open Microsoft.VisualBasic.FileIO [<EntryPoint>] let main argv = let csv_fileH = new TextFieldParser("test1

Using a `let` binding to increase a values lifetime

£可爱£侵袭症+ 提交于 2019-11-30 03:02:46
问题 I wrote the following code to read an array of integers from stdin : use std::io::{self, BufRead}; fn main() { let stdin = io::stdin(); for line in stdin.lock().lines() { let xs: Vec<i32> = line.unwrap() .trim() .split(' ') .map(|s| s.parse().unwrap()) .collect(); println!("{:?}", xs); } } This worked fine, however, I felt the let xs line was a bit long, so I split it into two: use std::io::{self, BufRead}; fn main() { let stdin = io::stdin(); for line in stdin.lock().lines() { let ss = line

Let vs. Binding in Clojure

人走茶凉 提交于 2019-11-29 19:15:24
I understand that they're different since one works for setting *compile-path* and one doesn't. However, I need help with why they're different. let creates a new scope with the given bindings, but binding ...? Brian Carper let creates a lexically scoped immutable alias for some value. binding creates a dynamically scoped binding for some Var . Dynamic binding means that the code inside your binding form and any code which that code calls (even if not in the local lexical scope) will see the new binding. Given: user> (def ^:dynamic x 0) #'user/x binding actually creates a dynamic binding for a

Why is assigning a value to the variable “let” possible?

自闭症网瘾萝莉.ら 提交于 2019-11-29 15:46:00
I am taking an online JS course and the instructor used the syntax : let = names = ["Bob","Tim","Larry"] . I am convinced that was an accident, but somehow allowed by the JS environment he was using. What he ended up doing (probably by accident) was assign the array to the "names" variable, which assigned it to the "let" variable. My question: why is this not an error? Why can "let" be used as an variable? We know "let" is a keyword. Or is it just something they haven't outlawed yet? You can still do it in the Chrome and Firefox console... and in Node, too, for that matter. In ES3 and earlier

Why are Clojure's `let` and `for` both monads?

坚强是说给别人听的谎言 提交于 2019-11-29 08:56:10
问题 In this discussion Brian Marick makes the point that let and for are monads in Clojure: That said, the really general-purpose monads tend to get written into the language as special forms. Clojure's let and for are both monads, but you don't need to know that to use them. This is let user=> (let [c (+ 1 2) [d e] [5 6]] (-> (+ d e) (- c))) 8 This is for user=> (for [x [0 1 2 3 4 5] :let [y (* x 3)] :when (even? y)] y) (0 6 12) My question is: Why are Clojure's let and for both monads? 回答1: Why

Clojure's 'let' equivalent in Scala

孤街醉人 提交于 2019-11-29 05:41:59
问题 Often I face following situation: suppose I have these three functions def firstFn: Int = ... def secondFn(b: Int): Long = ... def thirdFn(x: Int, y: Long, z: Long): Long = ... and I also have calculate function. My first approach can look like this: def calculate(a: Long) = thirdFn(firstFn, secondFn(firstFn), secondFn(firstFn) + a) It looks beautiful and without any curly brackets - just one expression. But it's not optimal, so I end up with this code: def calculate(a: Long) = { val first =

Does 'let' override a global declaration and throws a ReferenceError?

跟風遠走 提交于 2019-11-29 03:58:58
I was going through the Difference between var and let documentation example and was testing that when an undeclared variable is invoked, the global scope automatically provides a declaration for it (that's why the following snippet does not throw an error in any of the variables): x = 3; console.log(x); (function() { y=x+39; })() console.log(y); However, when one variable is declared with let after the assignment in the same global scope: x=3; let x = 42; console.log(x); One of the following errors is thrown: ReferenceError : x is not defined (Chromium) ReferenceError : can't access lexical

Why is var not deprecated?

时光总嘲笑我的痴心妄想 提交于 2019-11-29 02:59:58
问题 Lately after ES6 released, many sources suggested that I use "const" and "let" instead of "var", and that I should stop using "var" in my JavaScript. What I wonder is, if "var" has no advantage over "let" in all points of view, then why didn't they just fix var, or even deprecate "var" instead of letting them go along side each other? 回答1: Backwards compatibility. You're right in saying there is no real advantage to using var over let - if you define them at the start of a function their