This post is a just a tid-bit for the use of xargs in bash.
If you can get a list of work to do from a file or stream, you can pipe these into xargs to do further work. An example of this, I’ve taken from here. This will find all of the *.bak in or below the current directory and delete them.
find .-name"*.bak"-type f -print | xargs /bin/rm -f
Extending the usage of xargs to incorporate the -i switch, you can replace “{}” with the line of text read in the preprended command.
Concurrent programming is hard. It’s made a lot easier with good tools and MVar is one of them. MVars is just a location for a value. It can contain a value or contain nothing and the API will block accordingly, providing a safe concurrent programming environment for mutable state.
From the Hackage page for Control.Concurrent.MVar:
An MVar t is mutable location that is either empty or contains a value of type t. It has two fundamental operations: putMVar which fills an MVar if it is empty and blocks otherwise, and takeMVar which empties an MVar if it is full and blocks otherwise.
Key Points
newEmptyMVar creates an MVar that has no value to begin with
newMVar creates an MVar that has an initial value
takeMVar returns the current value of the MVar. It’ll block until the MVar contains a value
putMVar puts a value into the MVar. It’ll block until the MVar doesn’t contain a value
An Example
importControl.ConcurrentimportControl.Concurrent.MVarmain::IO()main=do-- create an empty mvarm<-newEmptyMVar-- get another thread to put a value in itforkIO$putMVarm"A value"-- take the valuex<-takeMVarmputStrLnx
When working with complex data structures in Haskell, burying down to observe a piece of information can be tedious. The Lens library has been created to ease this problem. From the Lens wiki page:
Lenses are composable functional references. They allow you to access and modify data potentially very deep within a structure!
The Lens library allows you to interact with your data structures in a composable manner, making your code easier to understand - and more fun to write.
Key Points
Whilst there are a lot of different functions, (^.) allows you to get some data and (.~) allows you to set some data
makeLenses is what does all the magic of creating your accessors
I haven’t found anywhere that specifically says this, but it seems that your fields in a record structure need to be preceded with an underscore
An Example
{-# LANGUAGE TemplateHaskell #-}importControl.LensdataBall=Ball{_position::(Double,Double),_velocity::(Double,Double)}deriving(Show)-- create the accessors for the Ball typemakeLenses''Ball-- | Animates a ball's position with respect to a timestep.-- Takes a ball's existing position and velocity and animates it-- by the time step provided--animate::Ball->Double->Ballanimatebt=doposition.~(px+vx*t,py+vy*t)$bwhere(px,py)=b^.position(vx,vy)=b^.velocitymain::IO()main=do-- the original ballletb=Ball{_position=(4.5,6.2),_velocity=(-0.3,1.2)}-- animate the ball by 1 full timestepletb'=animateb1putStrLn$"Initial ball : "++(showb)putStrLn$"Animated ball: "++(showb')
A very quick post that is based on the information from this stack overflow article on how to get a GHCi session up and running with the libraries referenced that you’ve installed using your cabal sandbox.
cd$YOUR_PACKAGE_DIR# For GHC >= 7.6
ghci -no-user-package-db-package-db .cabal-sandbox/i386-linux-ghc-7.6.1-packages.conf.d
# For GHC < 7.6
ghci -no-user-package-conf-package-conf .cabal-sandbox/i386-linux-ghc-7.4.2-packages.conf.d