formatting 'nil'

Q&A's, tips, howto's
Locked
jopython
Posts: 123
Joined: Tue Sep 14, 2010 3:08 pm

formatting 'nil'

Post by jopython »

I want the formatted output to look like follows for a csv file

Code: Select all

apple,,,peach
But I get an error.

Code: Select all

> (setq fruits '("apple" nil nil "peach"))
("apple" nil nil "peach")
> (format "%s,%s,%s,%s" fruits)

ERR: data type and format don't match in function format : nil
How to tell format to deal with an empty data type like nil

bairui
Posts: 64
Joined: Sun May 06, 2012 2:04 am
Location: China
Contact:

Re: formatting 'nil'

Post by bairui »

There might be a shorter way to express this, but my limited (new)lisp-fu offers you:

Code: Select all

(format "%s,%s,%s,%s" (map (fn (x) (or x "")) fruits))

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Re: formatting 'nil'

Post by cormullion »

That's good, bairui. Similar is:

Code: Select all

(format "%s,%s,%s,%s" (replace nil fruits ""))

jopython
Posts: 123
Joined: Tue Sep 14, 2010 3:08 pm

Re: formatting 'nil'

Post by jopython »

Thanks bairui and cormullion.

m i c h a e l
Posts: 394
Joined: Wed Apr 26, 2006 3:37 am
Location: Oregon, USA
Contact:

Re: formatting 'nil'

Post by m i c h a e l »

cormullion's solution is considerably faster:

Code: Select all

> (time (replace nil fruits "") 1000000)
173.114
> (time (map (fn (x) (or x "")) fruits) 1000000)
3435.875
> _
> 
m i c h a e l

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Re: formatting 'nil'

Post by cormullion »

Hey michael! Your second post this year - glad you still visit!

Yes, mapping anonymous functions is always going to be slower.

m i c h a e l
Posts: 394
Joined: Wed Apr 26, 2006 3:37 am
Location: Oregon, USA
Contact:

Re: formatting 'nil'

Post by m i c h a e l »

cormullion,

Yes, I'm still here, lurking in the shadows. I don't do much programming lately, but I'm still an enthusiastic newlisper!

bairui
Posts: 64
Joined: Sun May 06, 2012 2:04 am
Location: China
Contact:

Re: formatting 'nil'

Post by bairui »

W00t! Learned about replace() today. Yay. :-) Thanks, cormullion.

It was interesting to see Michael's speed test. I hadn't thought about how slow map() might be. And what a speed difference!

Another thing to note about the two different solutions, though, is that the map() is non-destructive, whereas replace() alters the original list.

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Re: formatting 'nil'

Post by cormullion »

since replace is so useful, you might sometimes want to use it non-destructively:

Code: Select all

(time (replace nil fruits "") 100000)
			11.262

(time (replace nil (copy fruits) "") 100000)
			63.958

(time (map (lambda (x) (or x "")) fruits) 100000)
			167.338

bairui
Posts: 64
Joined: Sun May 06, 2012 2:04 am
Location: China
Contact:

Re: formatting 'nil'

Post by bairui »

I should have thought about copy(). I have to do that all the time in Vim because its map() function is destructive. :-/

Thanks for the reminder, and speed test, Cormullion. Completed this thread nicely, I'd say. :-)

Locked