Did you know ... Search Documentation:
SWI-Prolog owl logo Predicate maplist/3
Availability::- use_module(library(apply)).(can be autoloaded)
Source maplist(:Goal, ?List1)
maplist(:Goal, ?List1, ?List2)
maplist(:Goal, ?List1, ?List2, ?List3)
maplist(:Goal, ?List1, ?List2, ?List3, ?List4)
True if Goal is successfully applied on all matching elements of the list. The maplist family of predicates is defined as:
maplist(G, [X_11, ..., X_1n],
 [X_21, ..., X_2n],
 ...,
 [X_m1, ..., X_mn]) :-
 call(G, X_11, ..., X_m1),
 call(G, X_12, ..., X_m2),
 ...
 call(G, X_1n, ..., X_mn).

This family of predicates is deterministic iff Goal is deterministic and List1 is a proper list, i.e., a list that ends in [].

Examples

Apply predicate to each element in a list

Apply predicate to each element in a list

?- maplist(plus(3), [1, X], [Y, 2]).
X = -1,
Y = 4.
Generate solutions

Generate solutions

?- length(L, 3), maplist(between(0, 1), L).
L = [0, 0, 0] ;
L = [0, 0, 1] ;
L = [0, 1, 0] ;
L = [0, 1, 1] ;
L = [1, 0, 0] ;
L = [1, 0, 1] ;
L = [1, 1, 0] ;
L = [1, 1, 1].
Fill a list

Fill a list

?- length(L, 5), maplist(=(foo), L).
L = [foo, foo, foo, foo, foo].
Test for atoms

Test for atoms

?- maplist(atom, [a, b, c]).
true.
?- maplist(atom, [a, 2, c]).
false.
Test for integers in a range

Test for integers in a range

?- maplist(between(1, 3), [1, 2, 3]).
true.
?- maplist(between(1, 3), [1, 2, 99]).
false.
Convert a CSV with column names to JSON

Convert a CSV with column names to JSON

With the help of library(csv) and library(json), we read a CSV file and use the column names in the first row to emit JSON.

For this example, we use the file weather.csv:

city,temp_lo,temp_hi,prcp,date
San Francisco,46,50,0.25,1994年11月27日
San Francisco,43,57,0,1994年11月29日
Hayward,37,54,,1994年11月29日

The script csv_to_json.pl reads the CSV from standard input or from the file provided as the first argument of the script. It converts the contents to a list of dicts, then writes this as JSON to standard output:

:- initialization(main, main).
:- use_module(library(csv)).
:- use_module(library(json)).
main :-
 ( current_prolog_flag(argv, [CSV_file|_])
 -> csv_read_file(CSV_file, CSV, [])
 ; csv_read_stream(current_input, CSV, [])
 ),
 CSV = [Colnames|Rows],
 Colnames =.. [row|Names],
 maplist(row_dict(Names), Rows, Dicts),
 json_write_dict(current_output, Dicts, [null('')]).
row_dict(Names, Row, Dict) :-
 Row =.. [row|Fields],
 pairs_keys_values(Data, Names, Fields),
 dict_create(Dict, _, Data).

Note: versions before 9.3.33 must use library(http/json) instead of library(json).

The null('') option to json_write_dict/3 is necessary to convert the empty "prcp" field in the last row to a missing value represented as null in the JSON output.

This is how we can use it to convert weather.csv to JSON:

$ swipl csv_to_json.pl weather.csv
[
 {
 "city":"San Francisco",
 "date":"1994-11-27",
 "prcp":0.25,
 "temp_hi":50,
 "temp_lo":46
 },
 {
 "city":"San Francisco",
 "date":"1994-11-29",
 "prcp":0,
 "temp_hi":57,
 "temp_lo":43
 },
 {
 "city":"Hayward",
 "date":"1994-11-29",
 "prcp":null,
 "temp_hi":54,
 "temp_lo":37
 }
]
Counting word frequency

Counting word frequency

word_frequency_count(Words, Counts) :-
 maplist(downcase_atom, Words, LwrWords),
 msort(LwrWords, Sorted),
 clumped(Sorted, Counts).
?- word_frequency_count([a,b,'A',c,d,'B',b,e], Counts).
Counts = [a-2, b-3, c-1, d-1, e-1].
See also
- tokenize_atom/2 or split_string/4 may be used to split a text into tokens
Tags are associated to your profile if you are logged in
Tags:
login to add a new annotation post.

AltStyle によって変換されたページ (->オリジナル) /