CoffeeScript comes with a few helper functions. How to use them? flatten(Array) for example.
These functions seem to be intended for private use by the CoffeeScript compiler. It might be better to use a general-purpose library like Underscore.js if you want these sort of features.
coffee> _ = require('underscore')
coffee> _.flatten [1, 2, 3, [4, 5]]
[ 1, 2, 3, 4, 5 ]
Related
I am trying to understand Sets in Swift and how to declare them correctly but I have found the following a little confusing.
I understand that this works:
let elements = [ 1, 2, 3, 4, 5, 1, 2, 6, 7]
let setFromElements = Set(elements)
But I don't understand why the following doesn't:
let setFromElements : Set = elements
Or even:
let setFromElements : Set<Int> = elements
When the following is valid:
let setFromArray : Set = [ 1, 2, 4, 5]
Can someone please help me understand why this is the case?
let setFromArray: Set = [ 1, 2, 4, 5] works because Set conforms to ExpressibleByArrayLiteral and hence has an initializer that takes an ArrayLiteral. See Set.init(arrayLiteral:). This conformance gives syntactic sugar for not having to explicitly call the init.
On the other hand, once you save the array literal into a variable using let elements = [ 1, 2, 3, 4, 5, 1, 2, 6, 7], elements becomes an Array, not an ArrayLiteral and hence another initializer of Set has to be called that takes an Array. This init does not provide syntactic sugar like ExpressibleByArrayLiteral does, so you explicitly have to call the init by doing Set(array).
Set has an initializer that takes an array, and that makes a set, by taking the unique items in the array. But a set is not an array, two different types, so you can't just use = to assign one to the other.
This question already has an answer here:
For loop for Dictionary don't follow it's order in Swift [duplicate]
(1 answer)
Closed 5 years ago.
the question that I want to ask, from which directions swift starts to read dictionaries or arrays
when I put some codes like this
let interestingNumber = [
"Square": [1,4,9,16,25],
"Prime": [2,3,5,7,11,13],
"Fiboannci": [1,1,2,3,5,8],
"asd":[2,3,4,5],
"zxc":[3,4,5]
]
for (key,values) in interestingNumber{
print(values)
}
the output is
[1, 4, 9, 16, 25]
[2, 3, 5, 7, 11, 13]
[1, 1, 2, 3, 5, 8]
[3, 4, 5]
[2, 3, 4, 5]
this is not the exact order, so do you know why swift does this ? and it sometimes makes it different too!
I guessed may be it does it in string order, then I tried but I think it is not too, so why swift does do that ?
Just like for NSDictionary, Swift dictionaries are not ordered by key or value. The order will always be unspecified.
If you need the keys to be sorted, your only option is to have an array of ordered keys, and use the for loop over this array.
From apple documentation (https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/CollectionTypes.html#//apple_ref/doc/uid/TP40014097-CH8-ID105)
Dictionaries
A dictionary stores associations between keys of the same type and
values of the same type in a collection with no defined ordering. Each
value is associated with a unique key, which acts as an identifier for
that value within the dictionary. Unlike items in an array, items in a
dictionary do not have a specified order. You use a dictionary when
you need to look up values based on their identifier, in much the same
way that a real-world dictionary is used to look up the definition for
a particular word.
Swift documentation says
debugDescription = A textual representation of self, suitable for debugging
description = A textual representation of self
in playground i get the output of both call as same
anArray.debugDescription // "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]"
anArray.description // "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]"
What is the actual difference between the two ?
From Apple documentation:
an object's debug description is the same as its description. However,
you can override debugDescription if you want to decouple these; many
Cocoa objects do this.
So basically unless you add any extra functionality to your debugDescription it will be the same as description.
For example, I need a valuable which is an Int, but I limited it can only set from 1-10. Is this features build in in swift? Except from override the setter. Is this possible to do so?
btw, what is this feature named? I remember I come across some languages got this features, but I don't recall the name of that languages.
You might be looking for an enumeration. It allows you to set a range of values (not strictly numbers though), which are allowed as input. Do something like this:
enum NumsTill10 {
case 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
}
And to set it as a variable:
let number:NumsTill10 = NumsTill10.1
Or even
let number:NumsTill10 = .1
And then you can do:
if number == .7 {
//It's 7!
}
I have exported matrix from Mathematica
Export["all.txt", all]
i.e.
Matrix is s.t
{{1,2,3,4},{1,2,3,4}}
I tried to import it back, but it is not usable..
Import["text.txt"]
{1, 2, 3}
{1, 2, 3}
,
Import["text.txt", "Data"]
{{"{1,", "2,", "3}"}, {"{1,", "2,", "3}"}}
or
Import["text.txt", "String"]
{1, 2, 3}
{1, 2, 3}
I cannnot use it because it is not matrix. How can I import it back as matrix? so I can get do some command like %[[1]][[1]] to obtain rows and column
There's nothing wrong with the file extension .txt, but when Mathematica exports and imports a text file, it assumes it is a string. You can see:
Export["C:\\test.txt", {{1, 2, 3}, {4, 5, 6}}]
Head[Import["C:\\test.txt"]]
(* Output: String *)
Instead, you can change the file extension as Eugene suggests:
Export["C:\\test.dat", {{1, 2, 3}, {4, 5, 6}}]
Head[Import["C:\\test.dat"]]
(* Output: List *)
Notice that it won't even use a file extension it doesn't understand:
Export["C:\\test.xyz", {{1, 2, 3}, {4, 5, 6}}]
Head[Import["C:\\test.xyz"]]
(* Output: Export::type: "List cannot be exported to the "XYZ" format." *)
Really, what's happening is Import and Export are trying to infer the type you want to use from the file extension. You can always specify that type manually instead:
Export["C:\\test.txt", {{1, 2, 3}, {4, 5, 6}},"List"]
Head[Import["C:\\test.txt"]]
(* Output: String *)
The output files for type "List" and type "String" are going to be the same, so you need to specify it when you import the file too:
Export["C:\\test.txt", {{1, 2, 3}, {4, 5, 6}},"List"]
Head[Import["C:\\test.txt","List"]]
(* Output: List *)
Note that this would work even if you forgot to Export with the type "List" specified, since the output file would still be compatible for importing as a list.
Worst case scenario, you can always try this:
Export["C:\\test.txt", {{1, 2, 3}, {4, 5, 6}}]
ToExpression[Import["C:\\test.txt"]]
(* Output: {4,5,6} *)
That doesn't quite work and only gives you bottom row. You'd have to be a bit pedantic:
Export["C:\\test.txt", {{1, 2, 3}, {4, 5, 6}}]
ToExpression[StringSplit[Import["C:\\test.txt"], "\n"]]
But of all these possibilities, the best and simplest I've mentioned is to simply tell the Import command that what you have is a list:
Import["C:\\test.txt","List"]
That's why that optional argument exists for Import in the first place. The reason it's optional is so we can be lazy and let Mathematica decide that .txt files are strings and .dat files are lists, but if you don't want Mathematica to do that (i.e. Mathematica is doing it wrong), you just specify "List" or whatever type of file you are importing.
You almost got this right but you used the type "Data" which is actually meant to indicate (from the documentation):
"data in a generic Wolfram Language form (list, string, etc.)"
which means you won't get much help there -- Mathematica will still decide this is a string. The type you want is "List".
I've noticed long time ago (since 6th versions) that Mathematica has some issues with *.txt files. Use suffix .dat. It has been proven issue-proof for me with matrices or anything.