Menu
  • HOME
  • TAGS

Haskell return lazy string from file IO

Tag: haskell,file-io,lazy-evaluation

Here I'm back again with a (for me) really strange behaviour of my newest masterpiece...

This code should read a file, but it doesn't:

readCsvContents :: String -> IO ( String )
readCsvContents fileName = do
     withFile fileName ReadMode (\handle -> do
          contents <- hGetContents handle
          return contents
          )

main = do
    contents <- readCsvContents "src\\EURUSD60.csv"
    putStrLn ("Read " ++ show (length contents) ++ " Bytes input data.")

The result is

Read 0 Bytes input data.

Now I changed the first function and added a putStrLn:

readCsvContents :: String -> IO ( String )
readCsvContents fileName = do
     withFile fileName ReadMode (\handle -> do
          contents <- hGetContents handle
          putStrLn ("hGetContents gave " ++ show (length contents) ++ " Bytes of input data.")
          return contents
          )

and the result is

hGetContents gave 3479360 Bytes of input data.
Read 3479360 Bytes input data.

WTF ??? Well, I know, Haskell is lazy. But I didn't know I had to kick it in the butt like this.

Best How To :

You're right, this is a pain. Avoid using the old standard file IO module, for this reason – except to simply read an entire file that won't change, as you did; this can be done just fine with readFile.

readCsvContents :: Filepath -> IO String
readCsvContents fileName = do
   contents <- readFile fileName
   return contents

Note that, by the monad laws, this is exactly the same1 as

readCsvContents = readFile

The problem with what you tried is that the handle is closed unconditionally when the monad exits withFile, without checking whether lazy-evaluation of contents has actually forced the file reads. That is of course horrible; I would never bother to use handles myself. readFile avoids the problem by linking the closing of the handle to garbage-collection of the original result thunk; this isn't altogether nice either but often works quite well.

For proper work with file IO, check out either the conduit or pipes library. The former focuses a bit more on performance, the latter more on elegance (but really, the difference isn't that big).


1And your first try is the same as readCsvContents fn = withFile fn ReadMode hGetContents.

How to handle the File hand-off from windows in a python program

python,windows,pdf,file-io

So, yes, Windows passes the file name into the script as one of the sys.argvs. It is (so far as I can tell from printing the value) a file name without the path, that I used to open the file, so that tells me that Windows starts my program with...

Keep track of loop without a counter

haskell

You can certainly do this without changing the type signature of func :: [Int] -> [Int]: have func call a different function, which takes an extra argument that is the counter you were talking about: func :: [Int] -> [Int] func = go 0 where go _ [] = []...

Get each fibbonacci value in haskell

haskell,fibonacci

Consider the simpler problem of summing the first 100 positive integers: sum [x | x <- [1,2..], x <= 100] This doesn't work either. As a human, you know that once x <= 100 returns False, it will never return True again, because x is getting larger. But Haskell doesn't...

Refactor an IO recursive loop into a monad folding in Haskell

sockets,haskell,network-programming,io-monad

The idiomatic way to repeat the same action over and over again forever is forever serverLoop :: Socket -> IO () serverLoop sock = forever $ do (conn, _) <- accept sock forkIO $ handleConn conn ...

apply a transformation with function inline

haskell

The read lambda applies to the first argument and the first argument to the function given to foldl is the accumulator. Those two arguments are the opposite for foldr. So, expanded, it looks like this: foldl (\acc element -> (read acc :: Int) + element) 0 ["10", "20", "30"] Since...

Recursion scheme in Haskell for repeatedly breaking datatypes into “head” and “tail” and yielding a structure of results

haskell,recursion

This looks like a special case of a (jargon here but it can help with googling) paramorphism, a generalisation of primitive recursion to all initial algebras. Reimplementing ListCase Let's have a look at how to reimplement your function using such a combinator. First we define the notion of paramorphism: a...

Haskell make recipe fails for Paradox theorem prover using GHC

linux,haskell,make,ghc,theorem-proving

Looks like paradox was written for a rather old version of GHC. You can fix all these "Could not find module" errors by using GHC version 7.8 or older and setting GHC = ghc -hide-package base -package haskell98 in the Makefile, though you will likely encounter more errors after that....

How to convert a Rational into a “pretty” String?

haskell,formatting,rational

Here's one that I wrote a few weeks ago. You can specify the number of decimals you want (correctly rounded), or just pass Nothing in which case it will print the full precision, including marking the repeated decimals. module ShowRational where import Data.List(findIndex, splitAt) -- | Convert a 'Rational' to...

logical expression evaluator Haskell

haskell

You're making eval a bit too low-level. By including Literals in the signature. A better way to do this is, is using recursion: eval :: Expression -> Bool eval (Literal x) = x eval (Operation AND x y) = (eval x) && (eval y) eval (Operation OR x y) =...

java.nio.file.NoSuchFileException: why nio not creating file [closed]

java,file-io,nio

Updated Answer: Now that you've shown the full code, there are two major problems: You're trying to open the file before ensuring that the directories leading up to it exist, and You're using StandardOpenOption.APPEND, but that won't create a file; it will append to an existing file. ...along with a...

Creating custom std::streambuf

c++,file-io,stl

It appears that trying to do this with std::ifstream will not work, and I need to change it std::istream. Once I make this change, the overloaded functions are called and everything works properly.

Stopping condition on a recursive function - Haskell

string,function,haskell,if-statement,recursion

Your code doesn't handle the case where a line is shorter than the maximum length. This is somewhat obscured by another bug: n is decremented until a whitespace is found, and then f is called recursively passing this decremented value of n, effectively limiting all subsequent lines to the length...

Why is f <$> g <$> x equivalent to (f . g) <$> x although <$> is not right-associative?

haskell,syntax,infix-notation,applicative,infix-operator

Why is f <$> g <$> x equivalent to (f . g) <$> x ...well, this isn't so much a functor-thing as a Haskell-thing. The reason it works is that functions are functors. Both <$> operators work in different functors! f <$> g is in fact the same as...

How do I avoid writing this type of Haskell boilerplate code

haskell,boilerplate

I assume that we'd like to have a solution for the general case where the changing type parameter is not necessarily in the right position for DeriveFunctor. We can distinguish two cases. In the simple case out data type is not recursive. Here, prisms are a fitting solution: {-# LANGUAGE...

Linux: Using split on limited space

linux,bash,file-io

My attempt: #! /bin/bash if [ $# -gt 2 -o $# -lt 1 -o ! -f "$1" ]; then echo "Usage: ${0##*/} <filename> [<split size in M>]" >&2 exit 1 fi bsize=${2:-100} bucket=$( echo $bsize '* 1024 * 1024' | bc ) size=$( stat -c '%s' "$1" ) chunks=$( echo...

Cancel last line iteration on a file

python,python-3.x,for-loop,file-io

In python3, the 'for line in file' construct is represented by an iterator internally. By definition, a value that was produced from an iterator cannot be 'put back' for later use (http://www.diveintopython3.net/iterators.html). To get the desired behaviour, you need a function that chains together two iterators, such as the chain...

How can I express foldr in terms of foldMap for type-aligned sequences?

haskell,types,monoids,type-variables,foldable

I found that this typechecks: {-# LANGUAGE RankNTypes #-} module FoldableTA where import Control.Category import Prelude hiding (id, (.)) class FoldableTA fm where foldMapTA :: Category h => (forall b c . a b c -> h b c) -> fm a b d -> h b d foldrTA ::...

Implementing map on a tree using fold

scala,haskell

Try to write your last line as def map(tree:Tree[Int])(f:Int=>Int) : Tree[Int] = fold(tree , EmptyTree:Tree[Int])((l,x,r) => Node(f(x),l,r)) Scala's type inference is very limited compared to haskell, in this case it tries to infere type of fold from it's arguments left to right, and incorectly decides that result type of fold...

Setting id and class with the haskell diagrams package

haskell,svg,haskell-diagrams

This cannot be done currently in diagrams, although it is something we would like to have in the future. You can get part of the way there using the diagrams-canvas backend, but that only displays on a local host and cannot be embedded into a web page. The only thing...

Thread blocked indefinitely in an MVar operation

haskell,concurrency,network-programming

Three days later and its solved: Was actually unrelated to either the networking or concurrency code, and infact caused by my incorrect re-implementation of Yampas dpSwitch in Netwire. Corrected code posted below for anyone wishing to implement this function: dpSwitch :: (Monoid e, Applicative m, Monad m, T.Traversable col) =>...

populate a 'JTable' with values from a '.txt' file

java,swing,file-io,jtable,stringtokenizer

you need to change your to something like this.you need to reset vector data = new Vector(); each time when you read new line otherwise it contain first row + second row + so on.and also you can call dtm.setRowCount(0); to avoid empty initial rows . and you need only...

First three items of a list in Haskell

haskell

Well, foo (x:y:z:xs) plus a “too short clause” certainly wouldn't be a bad solution. Another would be foo xs = case splitAt 3 xs of ([x,y,z],xs') -> calc x y z : foo (y:z:xs') _ -> [] Or, perhaps nicest, import Data.List (tails) foo xs = [ calc x y...

skip method in CipherInputStream

java,file-io,cryptography,inputstream

Found a solution that worked for me. Created a wrapper class that extended FilterInputStream and implemented the skip method using the same code found in InputStream.java Wrapper class public class SkipInputStream extends FilterInputStream { private static final int MAX_SKIP_BUFFER_SIZE = 2048; /** * Creates a <code>FilterInputStream</code> * by assigning the...

Haskell: When declaring a class, how can I use a type variable that is not immediately in the constructors?

haskell

using TypeFamilies The problem is that you somehow have to connect b with your collection (the elements in it) - there are several ways to do this but I think a rather nice one is using TypeFamilies: {-# LANGUAGE TypeFamilies #-} module Test where import qualified Data.Map as Map import...

IO Monad Example

haskell

The code you posted desugars into the following. x >>= (\a -> print a >> return 500) Or, expanding out the definition of (>>) x >>= (\a -> print a >>= (\_ -> return 500)) Then, you can see that in the different calls to (>>=), the types a and...

Fold over a heterogeneous, compile time, list

haskell,type-level-computation,hlist

Answering your comment: Actually, I can do if I can filter the heterogeneous list by type. Is that possible? You can filter the heterogeneous list by type if you add a Typeable constraint to b. The main idea is we will use Data.Typeable's cast :: (Typeable a, Typeable b) =>...

Hadoop map reduce Extract specific columns from csv file in csv format

java,hadoop,file-io,mapreduce,bigdata

You can Use Apache Pig to do Filtering and Validating Date format as well. Follow below steps : Copy your file into HDFS Load file using load command and pigStorage(). Select 20 column using ForEach statment (You can just give column name/number like $0,$3,$5..etc) Write UDF to validate date format...

Haskell IO - read from standard input directly to list

haskell

You may write: main = readLn >>= print . subsequences You will need to nail down the type to be read, for example by having a monomorphic subsequences or by annotating readLn. In ghci: Data.List> (readLn :: IO [Integer]) >>= print . subsequences [1,2,3] [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]] (I typed in the first...

Haskell do clause with multiple monad types

haskell,monads

A do block is for a specific type of monad, you can't just change the type in the middle. You can either transform the action or you can nest it inside the do. Most times transformations will be ready for you. You can, for instance have a nested do that...

Cannot access LocalFolder files on Windows phone app (UnauthorizedAccessException)

c#,file-io,windows-phone

I guess you're not disposing the StreamWriter. See the example on MSDN. using( var writer = new StreamWriter(await file.OpenStreamForWriteAsync())) { writer.WriteLine("Hello World"); writer.WriteLine("Goodbye world"); } That's why your can't read the file, because it's already taken by SreamWriter....

Replace all [ ] with {} - as short as possible [on hold]

haskell

All you need is love and to split print into putStrLn . show and then add a simple map in-between which does the conversion: main :: IO () main = let fn '[' = '{' fn ']' = '}' fn c = c in (readLn :: IO [Integer]) >>= putStrLn...

How to “wrap” monadic return value

haskell,monads

createNotificationIdentifierItem :: APNSIdentifier -> APNSItem createNotificationIdentifierItem (Identifier identifier) = Item $ do putWord8 3 putWord16be 4 putWord32be identifier or createNotificationIdentifierItem :: APNSIdentifier -> APNSItem createNotificationIdentifierItem (Identifier identifier) = do Item $ putWord8 3 Item $ putWord16be 4 Item $ putWord32be identifier after making APNSItem an instance of Monad (you can...

Troubles reading a single character from a text file

java,file-io

You need to check empty string before char operation. while ((line = bufferedReader.readLine()) != null) { if("".equals(line)){ continue; } char h = line.charAt(7); System.out.println(h); } ...

How can I get the location of a user's Documents directory?

c#,file-io

It depends on where you want to write it, which isn't completely clear by your question. If you're always looking for the current user's documents, then you should look at the System.Environment.GetFolderPath method and the System.Environment.SpecialFolder.MyDocuments enumeration value in particular. When that executes, it will give you the path to...

C++ basic fileIO locations

c++,osx,file-io

The program tries to find the file you open in the current working directory, not from the location where the executable file is. When you're executing a program from a terminal the current directory is the directory you're in. So if you do e.g. cd ~/ to go to your...

How does Frege generalize number literals?

haskell,frege

Simple decimal literals without type indicator (i.e. one of the letters lndf) do not automatically have type Int in Frege. They will get assigned the type you probably wanted, and the literal will get adapted accordingly. This is why they are called DWIM (do what I mean) literals. This is...

Haskell - generate and use the same random list

haskell,random

Here is a simple example (@luqui mentioned) you should be able to generalize to your need: module Main where import Control.Monad (replicateM) import System.Random (randomRIO) main :: IO () main = do randomList <- randomInts 10 (1,6) print randomList let s = myFunUsingRandomList randomList print s myFunUsingRandomList :: [Int] ->...

How do I load data from a txt file into variables in my C program?

c,arrays,parsing,file-io,struct

I won't be writing the code, but can try to help with the algotihm, in general. Open the file. Help : fopen() Check for successful opening. Hint: Return value. Read a line from the file. Check for the success. Help : fgets() and Return value. Based on data pattern ,...

Writing floats to a binary file in C++ | Equivalent of Java's DataOutputStream.writeFloat()

java,c++,file-io,binary

Java's default behaviour is to write files (and any other data streamed to IO) in big endian. C++ writes files in native format. On a standard x86-derived PC native is little endian, so for 0.5 you get: 0x00 00 00 3f in C++ and 0x3f 00 00 00 in Java....

issues with installing newer cabal version for haskell vim now

ubuntu,haskell,vim,ubuntu-14.04,cabal

Your $PATH variable seems to be broken. In a comment you said it was /home/me/google-cloud-sdk/bin:/.cabal/bin:/usr/local/sbin:/usr/local/bin:/usr/sb‌​in:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games This means that your shell (assumed to be bash) will look in the following directories /home/me/google-cloud-sdk/bin /.cabal/bin /usr/local/sbin /usr/local/bin /usr/sb‌​in /usr/bin /sbin /bin /usr/games /usr/local/games when looking for executable. If you look at the second...

Idiomatic list construction

list,haskell,functional-programming,idiomatic

The multiple call to addPoints could be replaced by a fold. As suggested in a comment, reversing your addPoint function would make things easier: addPoint' :: Point -> Polyline -> Polyline addPoint' p line = p:line So then your constructLine function could build a temporary list of the points to...

Haskell return lazy string from file IO

haskell,file-io,lazy-evaluation

You're right, this is a pain. Avoid using the old standard file IO module, for this reason – except to simply read an entire file that won't change, as you did; this can be done just fine with readFile. readCsvContents :: Filepath -> IO String readCsvContents fileName = do contents...

When I write to two files in python, the second one is always empty

python,file-io

I'm guessing here, but your problem is likely that stuff1 and stuff2 are pointing to the same generator or iterator, and you can only iterate through once. After that, its contents are exhausted. Try this instead: import itertools stuff1, stuff2 = itertools.tee(the_source_generator_or_iterator_or_whatever) with open('file1.txt', 'w') as file1: for thing in...

Hook into GHC runtime system

haskell,functional-programming,runtime,transactional-memory

(# s2#, TVar tvar# #) is an unboxed tuple. The name stg_newTVarzh is built from: The stg_ prefix, which is common to the whole GHC runtime, and stands for the spineless-tagless G-machine, an abstract machine to evaluate functional languages; newTVar which is the first part of newTVar#; the final zh,...

Combining Event and an attribute in threepenny-gui

haskell,threepenny-gui

This is intentional: The UI.checkedChange event only triggers when the user clicks the checkbox, but not when it is set programmatically. The intention is that the bBool behavior represents the canonical state of the checkbox and the UI.checkedChange event represents request from the user to change it, which may or...

Best practice for handling data types from 3rd party libraries in Haskell?

haskell,types

I am not ready to give a nice list of best practises, but for starters if you want to keep stuff sanely organized, use explicit exports instead of just exporting everything, e.g: module Parser ( parseConfig ) where ... Explicit exports also allow you to reexport your imports, e.g. module...

Decremented value called in the recursion in Haskell

string,function,haskell,recursion,parameters

Yes, once you call again f with a new value of n, it has no way to reference the old value of n unless you pass it explicitly. One way to do it is to have an internal recursive function with its width parameter, as you have, but that can...

Java FileOutputStream Created file Not unlocking

java,file-io

You are creating two instances of FileOutputStream and assigning both to out, but only closing one. Remove the second out = new FileOutputStream(new File(uploadedFileLocation));. Also, you should consider using a try-with-resources block private void writeToFile(InputStream uploadedInputStream, String uploadedFileLocation) { try (OutputStream out = new FileOutputStream(new File(uploadedFileLocation))) { int read =...

Getting Apache Camel to stop retrying if failed to move the file after route completion

java,file-io,exception-handling,locking,apache-camel

This is something that the File component needs to handle, not your route. You can configure how the File component should handle locked files using the readLock option (along with other related options). The option has an extensive description on the File2 component documentation page...