[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.lisp

Re: MAP (and variants) vs LOOP - Popular opinion observation?

William James

11/4/2015 12:47:00 PM

Nathan Baum wrote:

> Then suppose you later need the loop/map to collect some of the values
> under certain conditions. You might have
>
> (loop for x in (get-list)
> for i from 0
> do (format t "~A - ~A~%" i x)
> if (test x)
> collect (foo x))

Gauche Scheme:

(use srfi-42 :only (list-ec))
(use srfi-13 :only (string-upcase))

(list-ec (: x (index i) '("a" "of" "I" "it"))
(begin (print i " - " x))
(if (#/../ x))
(string-upcase x))

0 - a
1 - of
2 - I
3 - it
("OF" "IT")


MatzLisp (Ruby):

%w(a of I it).each_with_index.map{|x,i|
puts "#{i} - #{x}"
x.match(/../) && x.upcase}.compact

0 - a
1 - of
2 - I
3 - it
==>["OF", "IT"]


Another way:

%w(a of I it).each_with_index.flat_map{|x,i|
puts "#{i} - #{x}"
x.match(/../) ? x.upcase : []}


Another way:

r=[]
%w(a of I it).each_with_index{|x,i|
puts "#{i} - #{x}"
r<<x.upcase if x.match(/../)}
r

--
According to some estimates, the rapidly growing Muslim immigrant population
may turn Malmo into a Muslim majority city within about ten years.... Native
Swedes are leaving the city in droves, as crime is rampant and the police
publicly admit they don't control all parts of the city. There are now gangs in
Malmo specialized in assaulting old people visiting the graves of relatives.
fjordman.blogspot.ca/2005/05/is-swedish-democracy-collapsing.html
2 Answers

William James

1/20/2016 5:49:00 AM

0

WJ wrote:

> Another way:
>
> %w(a of I it).each_with_index.flat_map{|x,i|
> puts "#{i} - #{x}"
> x.match(/../) ? x.upcase : []}

Shorter:

%w(a of I it).flat_map.with_index{|x,i|
puts "#{i} - #{x}"
x.size==2 ? x.upcase : []}
===>
0 - a
1 - of
2 - I
3 - it
==>["OF", "IT"]

--
77.6 percent of the country's rapists are identified as "foreigners".... And
even this likely understates the issue, since the Swedish government---in an
effort to obscure the problem---records second-generation Muslim perpetrators
simply as "Swedes." http://redicecreations.com/article.ph...

William James

4/22/2016 9:03:00 PM

0

WJ wrote:

> Nathan Baum wrote:
>
> > Then suppose you later need the loop/map to collect some of the values
> > under certain conditions. You might have
> >
> > (loop for x in (get-list)
> > for i from 0
> > do (format t "~A - ~A~%" i x)
> > if (test x)
> > collect (foo x))
>
> Gauche Scheme:
>
> (use srfi-42 :only (list-ec))
> (use srfi-13 :only (string-upcase))
>
> (list-ec (: x (index i) '("a" "of" "I" "it"))
> (begin (print i " - " x))
> (if (#/../ x))
> (string-upcase x))
>
> 0 - a
> 1 - of
> 2 - I
> 3 - it
> ("OF" "IT")
>
>
> MatzLisp (Ruby):
>
> %w(a of I it).each_with_index.map{|x,i|
> puts "#{i} - #{x}"
> x.match(/../) && x.upcase}.compact
>
> 0 - a
> 1 - of
> 2 - I
> 3 - it
> ==>["OF", "IT"]
>
>
> Another way:
>
> %w(a of I it).each_with_index.flat_map{|x,i|
> puts "#{i} - #{x}"
> x.match(/../) ? x.upcase : []}
>
>
> Another way:
>
> r=[]
> %w(a of I it).each_with_index{|x,i|
> puts "#{i} - #{x}"
> r<<x.upcase if x.match(/../)}
> r

OCaml:

["a"; "of"; "I"; "it"]
|> List.mapi (fun i s -> i,s)
|> List.fold_left
(fun acc (i,s) ->
Printf.printf "%d - %s\n" i s;
if String.length s = 2 then String.uppercase s :: acc else acc)
[] ;;


0 - a
1 - of
2 - I
3 - it
- : string list = ["IT"; "OF"]

--
"Anti-racism," Shamir writes, "is a denial of the autochthon's [native's] right
to decide his fate; a tool to separate Man from his native landscape. This
concept de-legitimizes objections to swamping a land with a flood of immigrants
and ruining the society's fabric."
www.theoccidentalobserver.net/authors/Connelly-Gaza2.html