www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Ammonite Scale library

reply "bearophile" <bearophileHUGS lycos.com> writes:
An interesting library for Scala language (found through Reddit):

https://github.com/lihaoyi/Ammonite#ammonite-010

I think this library is interesting for D not for its command 
line usage nor for the details of its syntax (that I think is too 
much succinct), but as an example of things you want to do in a 
handy way with Phobos, as:


// Get the current working directory
val d = cwd

// Copy a file or folder
cp(d/'file, d/'file2)

// Make a folder named "folder"
mkdir! d/'folder

// Rename all .scala files inside the folder d into .java files
ls! d | mv{case r"$x.scala" => s"$x.java"}

// Line-count of all .scala files recursively in d
ls.rec! cwd |? (_.ext == "scala") | read.lines | (_.size) sum

// Find and concatenate all .js files directly in the working 
directory
ls! cwd |? (_.ext == "js") | read |> write! wd/'target/"bundle.js"

// Make a directory
mkdir! cwd/'folder

// Move it
mv(cwd/'folder, cwd/'folder2)

// Copy it; recursive by default
cp(cwd/'folder2, cwd/'folder3)

// List the files in cwd
ls! cwd

// List the files in cwd recursively
ls.rec! cwd

// Remove one of them; recursive by default
rm! cwd/'folder2

// Write a file, if it doesn't exist
write(cwd/'folder2/"data.txt", "I am a cow")

// Write to a file, stomping over anything that exists
write.over(cwd/'folder2/"data.txt", "I am a cow")

// Append to a file
write.append(cwd/'folder2/"data.txt", "I am a cow")

// Read a file as a String
read! cwd/'folder2/"data.txt"

// Read a file as an Iterator[String]
read.lines! cwd/'folder2/"data.txt"

// Read a file as an Array[Byte]
read.bytes! cwd/'folder2/"data.bin"

// Check if a file or folder exists
exists! cwd/'folder2/"data.txt"


Bye,
bearophile
Jan 18 2015
parent reply "Tobias Pankrath" <tobias pankrath.net> writes:
What's up with that 'filename syntax?
Jan 18 2015
next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
Tobias Pankrath:

 What's up with that 'filename syntax?
I think such little details are not important for this Phobos thread. Bye, bearophile
Jan 18 2015
prev sibling parent Dmitry Olshansky <dmitry.olsh gmail.com> writes:
On 18-Jan-2015 17:35, Tobias Pankrath wrote:
 What's up with that 'filename syntax?
If memory serves right it's Symbol in Scala which is about the same as string constant. The assumption is that compiler is aware of all symbols used in a program and keeps them interned at all times. http://stackoverflow.com/questions/3554362/purpose-of-scalas-symbol Differences are minor, I think the snippet tries to show that the library supports both. I guess the code works by having an implicit conversion from String and Symbol to some PathSegment type from string and overloading / operator for PathSegments. Since there is no / for basic strings Scala as usual tries all implicit conversions of arguments to find the proper method. All of this implicit conversion to some other type on demand may sound scary and is very much unlike D's way of extending existing types with UFCS. IMHO it's more powerful but more prone to abuse. -- Dmitry Olshansky
Jan 18 2015