Skip to content

Instantly share code, notes, and snippets.

@AlephAlpha
Last active July 17, 2018 13:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlephAlpha/cf198c368d82aa4c74550e653c91d8ff to your computer and use it in GitHub Desktop.
Save AlephAlpha/cf198c368d82aa4c74550e653c91d8ff to your computer and use it in GitHub Desktop.
10 Mathematica One Liners to Impress Your Friends

Inspired by Marcus Kazmierczak's 10 Scala One Liners to Impress Your Friends.

1. Multiple Each Item in a List by 2

Range[10] * 2

2. Sum a List of Numbers

Fold[Plus, Range[1000]]

Total[Range[1000]]

3. Verify if Exists in a String

wordList = {"coffeescript", "eko", "play framework", "and stuff", "falsy"}
tweet = "This is an example tweet talking about javascript and stuff."

StringContainsQ[tweet, wordList]

4. Read in a File

fileText = Import["data.txt"]

fileLines = Import["data.txt", "List", "Numeric" -> False]

5. Happy Birthday to You!

Do[Print["Happy Birthday " <> If[i == 3, "dear NAME", "to You"]], {i, 4}]

6. Filter list of numbers

{passed, failed} = Lookup[GroupBy[{49, 58, 76, 82, 88, 90}, # > 60 &], {True, False}, {}]

7. Fetch and Parse an XML web service

results = Import["http://search.twitter.com/search.atom?&q=scala", "XML"]

8. Find minimum (or maximum) in a List

Min[{14, 35, -7, 46, 98}]
Max[{14, 35, -7, 46, 98}]

9. Parallel Processing

result = ParallelMap[processItem, dataList]

10. Sieve of Eratosthenes

Reap[Range[2, #] //. p : {x_, ___} :> Complement[p, Range[0, #, Sow[x]]]][[2, 1]] &
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment