Hi Jonathan—point by point: Functions
- All functions take the form
FuncName[arg1,arg2,...] (they could also have zero arguments). The arguments could really be anything at all, from integers to strings to lists to images to videos to neural nets.
- When you get an error message, read it carefully—it's usually pretty instructive. You might see a red little set of ellipses by the cell bracket; click that to get more info.
- Finding the best functions is like finding the best words when constructing a sentence: you can only get there by practicing and by reading. In this case, I recommend just trying things out—if they don't work, or you think they could be better, go to the documentation and read about the basic examples, the scope, and the neat examples. Most importantly, check out the Related Functions at the bottom, because this is where you'll often find a function that does the job better if what you're currently using doesn't seem quite right.
ShortcutsI think that the links in today's review notebook (available in the course materials folder) should help you out here. Best PracticesWe'll talk about best practices, tips and tricks, and so on in the coming sessions. In the meantime, a few basic tips:
- You have full access to text cells, so use 'em! You can explain what each chunk of your code does while keeping the notebook looking clean and stylish.
- Be careful about writing ultra-cool compact code. It is indeed very neat, but it can be hard to decipher. Break things up and space them out to a degree you think is reasonable, so long as performance isn't too negatively affected. I don't do this for my own personal code and I can wind up with stuff like this:
StringTake[
LongestCommonSubsequence @@ StringTakeDrop[#, StringLength@#/2],
1] & /@ StringSplit[d3dat, "\n"] /.
Thread[#~Join~Capitalize@# &@Alphabet[] -> Range@52] // Total
or this:
Boole@*Or @@
SubsetQ @@@ {#, Reverse@#} & /@ (Range @@@
ToExpression@StringPart[{##}, {1, 3}] & @@@
StringSplit[
StringSplit[
dat, "\n"],
","]) // Total
Don't be like me!
EntitiesThe best way to see if what you're looking for might exist in entity form is—for my money—just by pressing ctrl+= and typing it in natural language. Otherwise, it can be a bit tricky to navigate until you're familiar with the types of entities which exist (which you could see by evaluating EntityValue , if you like). Once you have an entity 'ent', you can ask for its properties by running ent["Properties"] . Some of the properties might not be available for your particular entity, but they'll still be listed here because they're available for other members of that type (e.g. think of some properties of elements [chemical elements, not programming elements!] which might exist for some elements but not others). Map and Related Functions(J., this will be relevant to you too, so hopefully you see it.)
Map simply takes some expression (generally a function) and "acts" it across a list at whatever level you desire (by default, at the top level). So: Map[f,{a,b,c}]=={f[a],f[b],f[c]} . It also has the shorthand /@ , so you could rewrite that as f/@{a,b,c} and get the same result.
- Mapping can be thought of as the primary way to avoid looping through lists (nested or not) when you have some operation that needs to be performed to the elements of a list.
MapApply , the recently-named full form of the diabolical-looking @@@ , maps Apply , for some expression f , to the elements of a list. In other words: MapApply[f,{g[x],g[y],g[z]}]=={f[x],f[y],f[z]} . You could also do, say: List@@@{a+b+c,x*y*z,g[1,2,3]} to get {{a,b,c},{x,y,z},{1,2,3}} .
MapThread (no shorthand that I'm aware of) takes a functional needle and uses it to thread together corresponding elements of two or more lists. A basic example: MapThread[f,{{a,b,c},{1,2,3}}]=={f[a,1],f[b,2],f[c,3]} . You can use pure anonymous functions as its first argument, like: MapThread[#1^#2+#3&,{{a,b,c},{1,2,3},{4,5,6}}] to get {a^1+4,b^2+5,c^3+6} .
Hopefully this helps!
|