Tag Archives: ceon

New and old news

It’s been a while since I wrote something, not without a reason. Last months were terribly busy for me. First of all, I defended my PhD (yay!). Second, a lot of my time was consumed by writing proposals. Third, upcoming changes to R package ‘igraph‘ caused an unscheduled but necessary review of some of my R packages. […]

Apache Hadoop w CEON, ICM UW

W ramach Laboratorium badawczego KDD (Knowledge Discovery in Databases) będącego częścią Centrum Otwartej Nauki, ICM UW pracujemy nad metodami przechowywania i analizy treści w dużych kolekcjach dokumentów (publikacji naukowych). Ja natomiast nazywam się Adam Kawa i jestem developerem Hadoop-a w zespole dr Łukasza Bolikowskiego zajmującego się tymi zagadnieniami. Niniejszy blog będzie miejscem, gdzie chciałbym się dzielić się naszymi [...]

SNA with R workshop at Sunbelt XXXII in Redondo Beach

I am currently in Redondo Beach, CA at the Sunbelt XXXII social networks conference. The program is thick from numerous interesting talks so the event promises to be very interesting. Today in the morning I gave the workshop “Introduction to Social Network Analysis with R”. Over 50 people registered. I am grateful to all the […]

Typoglycemia in Haskell

Can you decipher the following sentences?All hmuan biegns are bron fere and euqal in dgiinty and rgiths. Tehy are ednwoed wtih raeosn and cnocseicne and sohlud act tworads one aonhter in a sipirt of borhtreohod.It’s the first article of the Universal D…

Assorted curiosities: Geography

Fun facts learned while clicking through Wikipedia:Treasure Island in Ontario, Canada is probably the largest island in a lake in an island in a lake.Liechtenstein and Uzbekistan are doubly landlocked countries, i.e., all the neighbouring countries are…

My first use of the state monad

While learning Haskell, I was looking for a concise implementation of a function which “reshapes” a list into a matrix.  Given the number of rows r, the number of columns c, and a list vs, the function should take r*c values from the list and create a r by c matrix out of them.  Here’s the type:

toMatrix :: Int -> Int -> [a] -> [[a]]

First solution

First I wrote  a simpler function that would split a list into chunks of a given size, like this:

chunksOf :: Int -> [a] -> [[a]]
chunksOf _ [] = []
chunksOf c vs = h : (chunksOf c t)
where (h, t) = splitAt c vs

Using the above, toMatrix could be implemented this way:

toMatrix r c = chunksOf c . take (r*c)

I had a feeling that a function like chunksOf should be already present somewhere in the standard library, so I asked Hoogle, but to no avail. There was chunksOf in Data.Text, but it operated on Text only (I retroactively named my function after the one in Data.Text).  However, Hoogle returned replicateM as well…

Second solution

… and I realized I could use it with the state monad to implement toMatrix.  The state could contain the list of values yet to be consumed, and the action to be replicated could be chopping off c values from the list:

splitOnce :: Int -> State [a] [a]
splitOnce c = do
s <- get
let (h, t) = splitAt c s
put t
return h

After a while I realized that the same function could be written in a much more concise form:

splitOnce' :: Int -> State [a] [a]
splitOnce' = state . splitAt

The solution was, therefore:

toMatrix r = evalState . replicateM r . state . splitAt

Summary

The second version is good enough for me and as a bonus it helped me understand the state monad. Note that the two implementations of toMatrix are not equivalent, as they handle lists shorter than r*c in different ways.  Future work: find a concise and preferably point-free implementation of chunksOf.

Update (2013-01-08)

This answer on StackOverflow contains a very nice implementation of chunksOf.

My first use of the state monad

While learning Haskell, I was looking for a concise implementation of a function which “reshapes” a list into a matrix.  Given the number of rows r, the number of columns c, and a list vs, the function should take r*c values from the list and create a r by c matrix out of them.  Here’s the type:

toMatrix :: Int -> Int -> [a] -> [[a]]

First solution

First I wrote  a simpler function that would split a list into chunks of a given size, like this:

chunksOf :: Int -> [a] -> [[a]]
chunksOf _ [] = []
chunksOf c vs = h : (chunksOf c t)
where (h, t) = splitAt c vs

Using the above, toMatrix could be implemented this way:

toMatrix r c = chunksOf c . take (r*c)

I had a feeling that a function like chunksOf should be already present somewhere in the standard library, so I asked Hoogle, but to no avail. There was chunksOf in Data.Text, but it operated on Text only (I retroactively named my function after the one in Data.Text).  However, Hoogle returned replicateM as well…

Second solution

… and I realized I could use it with the state monad to implement toMatrix.  The state could contain the list of values yet to be consumed, and the action to be replicated could be chopping off c values from the list:

splitOnce :: Int -> State [a] [a]
splitOnce c = do
s <- get
let (h, t) = splitAt c s
put t
return h

After a while I realized that the same function could be written in a much more concise form:

splitOnce' :: Int -> State [a] [a]
splitOnce' = state . splitAt

The solution was, therefore:

toMatrix r = evalState . replicateM r . state . splitAt

Summary

The second version is good enough for me and as a bonus it helped me understand the state monad. Note that the two implementations of toMatrix are not equivalent, as they handle lists shorter than r*c in different ways.  Future work: find a concise and preferably point-free implementation of chunksOf.

Update (2013-01-08)

This answer on StackOverflow contains a very nice implementation of chunksOf.

Package ‘intergraph’ (1.1-0) released!

I just released the first official version of the ‘intergraph’ R package. With the functions provided in the current version (1.1-0) you can convert network data objects between classes ‘igraph’ and ‘network’. The package supports directed and undirected networks, and handles the node, tie, and network (graph) attributes. Mutliplex networks (i.e., with possibly multiple ties […]

Shortest paths to/from nodes of a certain type

Elijah asked the following via SOCNET mailing list: I was wondering if anyone knew of a script or tool which would give me the network distance of nodes to a particular class of nodes.  I think of this as an Erdos number, except instead of getting the distance to one node, I want the distance […]

Social contagion story update

July last year I wrote a note about the stream of papers by Nicholas Christakis and James Fowler (or CF) and coauthors on social contagion of many things (obesity, smoking, loneliness to name the few). I also wrote about a paper by Russel Lyons that provided a detailed critique of the analyzes presented in the […]