Warning: strpos() [function.strpos]: Empty needle in /home/dealer/kasat/pkg/cahir/web/html/lib/plugins/translation2/action.php on line 53

Warning: Cannot modify header information - headers already sent by (output started at /home/dealer/kasat/pkg/cahir/web/html/lib/plugins/translation2/action.php:53) in /home/dealer/kasat/pkg/cahir/web/html/inc/actions.php on line 163
erlang:stdlib [Krystian Bacławski Wiki]
 

Listy - moduł lists

Przykładowe listy

> L1 = [ 1 , 2 , 3 , 4 , a , b , c , "ala" , "ma" , "kota" ].
[1,2,3,4,a,b,c,"ala","ma","kota"]
> L1A = [ [ 1 , 2 , 3 , 4 ] , [ a , b , c ] , [ "ala" , "ma" , "kota" ] ].
[[1,2,3,4],[a,b,c],["ala","ma","kota"]]
> L2 = lists:seq( 1 , 10 ).
[1,2,3,4,5,6,7,8,9,10]
> L3 = [ a , b , c , true , false ].
[a,b,c,true,false]
> L4 = [ 3 , 6 , 9 , 8 , 4 , 2 , 1 , 5 , 7 ].
[3,6,9,8,4,2,1,5,7]

Podstawowe operacje

 1: > lists:append( L1 , [ "bialego" ] ). % operator ++
 2: [1,2,3,4,a,b,c,"ala","ma","kota","bialego"]
 3: > lists:delete( "kota" , L1 ).
 4: [1,2,3,4,a,b,c,"ala","ma"]
 5: > lists:member( "ala" , L1 ).
 6: true
 7: > lists:member( "ala" , L1A ).
 8: false
 9: > lists:subtract( L1 , [ 1 , a , "kota" ] ). % operator --
10: [2,3,4,b,c,"ala","ma"]
11: > lists:reverse( L2 ).
12: [10,9,8,7,6,5,4,3,2,1]
13: > lists:reverse( L2 , [ 0 , - 1 , - 2 ] ).
14: [10,9,8,7,6,5,4,3,2,1,0,-1,-2]
15: > lists:sort( L4 ).
16: [1,2,3,4,5,6,7,8,9]
17: > lists:flatten( L1A ).
18: [1,2,3,4,a,b,c,97,108,97,109,97,107,111,116,97]
19: > length ( L1A ).
20: 3
21: > lists:flatlength( L1A ).
22: 16
23: > lists:min( L4 ).
24: 1
25: > lists:max( L4 ).
26: 9
27: > lists:sum( L4 ).
28: 45
29: > lists:last( L4 ).
30: 7
31: > lists:nth( 5 , L1 ).
32: a
33: > lists:nthtail( 5 , L1 ).
34: [b,c,"ala","ma","kota"]
35: > lists:split( 4 , L1 ).
36: {[1,2,3,4],[a,b,c,"ala","ma","kota"]}
37: > lists:sublist( L1 , 4 ).
38: [1,2,3,4]
39: > lists:sublist( L1 , 5 , 3 ).
40: [a,b,c]
41: > lists:merge( [ 1 , 3 , 5 , 7 , 9 ] , [ 2 , 4 , 6 , 8 , 10 ] ).
42: [1,2,3,4,5,6,7,8,9,10]
43: > lists:prefix( "foo" , "foobar" ).
44: true
45: > lists:prefix( "bar" , "foobar" ).
46: false
47: > lists:suffix( "foo" , "foobar" ).
48: false
49: > lists:suffix( "bar" , "foobar" ).
50: true

Tworzenie, łączenie, złączanie

 1: > lists:duplicate( 20 , $- ).
 2: "--------------------"
 3: > lists:seq( 10 , 20 ).
 4: [10,11,12,13,14,15,16,17,18,19,20]
 5: > lists:seq( 0 , 100 , 10 ).
 6: [0,10,20,30,40,50,60,70,80,90,100]
 7: > lists:append( L1A ).
 8: [1,2,3,4,a,b,c,"ala","ma","kota"]
 9: > lists:concat( [ "ala_" , a , "_ma_" , 1 , "_kota" ] ).
10: "ala_a_ma_1_kota"
11: > lists:merge( "aclgrt" , "bdfnz" ).
12: "abcdflgnrtz"
13: > LA = [ a , b , c ].
14: [a,b,c]
15: > LB = [ 1 , 2 , 3 ].
16: [1,2,3]
17: > LC = [ "ala" , "ma" , "kota" ].
18: ["ala","ma","kota"]
19: > Z2 = lists:zip( LA , LB ).
20: [{a,1},{b,2},{c,3}]
21: > Z3 = lists:zip3( LA , LB , LC ).
22: [{a,1,"ala"},{b,2,"ma"},{c,3,"kota"}]
23: > lists:unzip( Z2 ).
24: {[a,b,c],[1,2,3]}
25: > lists:unzip3( Z3 ).
26: {[a,b,c],[1,2,3],["ala","ma","kota"]}
27: > lists:zipwith( fun(X,Y) -> lists:concat([Y,"-",X]) end, LA, LB ).
28: ["1-a","2-b","3-c"]

Funkcje wyższego rzędu

 1: > lists:all( fun(X) -> is_integer( X ) end , L1 ).
 2: false
 3: > lists:any( fun(X) -> is_list( X ) end , L1 ).
 4: true
 5: > lists:foreach( fun(X) -> io : format( "~p~n" , [ X ] ) end , L3 ).
 6: a
 7: b
 8: c
 9: true
10: false
11: ok
12: > lists:filter( fun(X) -> is_list( X ) end , L1 ).
13: ["ala","ma","kota"]
14: > lists:map( fun(X) -> X * X end , L2 ).
15: [1,4,9,16,25,36,49,64,81,100]
16: > lists:sort( fun(X,Y) -> X > Y end , L2 ).
17: [10,9,8,7,6,5,4,3,2,1]
18: > lists:dropwhile( fun ( X ) -> is_integer( X ) end , L1 ).
19: [a,b,c,"ala","ma","kota"]
20: > lists:takewhile( fun ( X ) -> is_integer( X ) end , L1 ).
21: [1,2,3,4]
22: > lists:splitwith( fun ( X ) -> not is_list( X ) end , L1 ).
23: {[1,2,3,4,a,b,c],["ala","ma","kota"]}
24: > lists:partition( fun( X ) -> X < 5 end , L2 ).
25: {[1,2,3,4],[5,6,7,8,9,10]}
26: > P = fun( A , B ) -> { A , B } end.
27: #Fun<erl_eval.12.113037538>
28: > lists:foldl( P , first , L2 ).
29: {10,{9,{8,{7,{6,{5,{4,{3,{2,{1,first}}}}}}}}}}
30: > lists:foldr( P , first , L2 ).
31: {1,{2,{3,{4,{5,{6,{7,{8,{9,{10,first}}}}}}}}}}

Listy krotek

 1: > TL1 = lists:zip3( [a,b,c], [1,2,3], ["ala","ma","kota"] ).
 2: [{a,1,"ala"},{b,2,"ma"},{c,3,"kota"}]
 3: > lists:keyfind( "ala" , 3 , TL1 ).
 4: {a,1,"ala"}
 5: > lists:keymember( "ala" , 3 , TL1 ).
 6: true
 7: > lists:keyfind( b , 2 , TL1 ).
 8: false
 9: > TL2 = lists:keyreplace( b , 1 , TL1 , { d , 4 , "nie ma" } ).
10: [{a,1,"ala"},{d,4,"nie ma"},{c,3,"kota"}]
11: > lists:keysort( 3 , TL2 ).
12: [{a,1,"ala"},{c,3,"kota"},{d,4,"nie ma"}]
13: > lists:keytake( d , 1 , TL2 ).
14: {value,{d,4,"nie ma"},[{a,1,"ala"},{c,3,"kota"}]}
15: > lists:keystore( b , 1 , TL1 , { e , 5 , "chyba ma" } ).
16: [{a,1,"ala"},{e,5,"chyba ma"},{c,3,"kota"}]
17: > TL3 = lists:keystore( b , 1 , TL2 , { e , 5 , "psa" } ).
18: [{a,1,"ala"},{d,4,"nie ma"},{c,3,"kota"},{e,5,"psa"}]
19: > lists:keydelete( "kota" , 3 , TL3 ).
20: [{a,1,"ala"},{d,4,"nie ma"},{e,5,"psa"}]
21: > lists:keymap( fun ( A ) -> atom_to_list( A ) end , 1 , TL2 ).
22: [{"a",1,"ala"},{"d",4,"nie ma"},{"c",3,"kota"}]
23: > TLA = lists:zip3( [a,b,c], [1,3,5], ["ala","maja","kota"] ).
24: [{a,1,"ala"},{b,3,"maja"},{c,5,"kota"}]
25: > TLB = lists:zip3( [d,e], [2,6], ["i chuck","lub kapibare"] ).
26: [{d,2,"i chuck"},{e,6,"lub kapibare"}]
27: > lists:keymerge( 2 , TLA , TLB ).
28: [{a,1,"ala"},{d,2,"i chuck"},{b,3,"maja"},{c,5,"kota"},{e,6,"lub kapibare"}]

Tablice - moduł array

Tworzenie, domyślna wartość pól, ustawianie

 1: > A0=array:new([{size,10},{fixed,false},{default,0}]).
 2: {array,10,10,0,10}
 3: > array:is_array(A0).
 4: true
 5: > array:default(A0).
 6: 0
 7: > A1=array:set(2,b,A0).
 8: {array,10,10,0,{0,0,b,0,0,0,0,0,0,0}}
 9: > A2=array:set(4,d,A1).
10: {array,10,10,0,{0,0,b,0,d,0,0,0,0,0}}
11: > A3=array:set(5,e,A2).
12: {array,10,10,0,{0,0,b,0,d,e,0,0,0,0}}

Pobieranie, wymazywanie, rozmiar

> array:get(5,A3).
e
> A4 = array:reset(2,A3).
{array,10,10,0,{0,0,0,0,d,e,0,0,0,0}}
> array:get(2,A4).
0
> array:size(A3).
10
> array:sparse_size(A3).
6
> A5=array:resize(A3).
{array,6,10,0,{0,0,b,0,d,e,0,0,0,0}}
> array:size(A5).
6

Tworzenie z listy i słownika

> NumList=lists:seq(0,9).
[0,1,2,3,4,5,6,7,8,9]
> AtomList=[a,b,c,d,e,f,g,h,i,j].
[a,b,c,d,e,f,g,h,i,j]
> A0=array:from_list(AtomList).
{array,10,10,undef,{a,b,c,d,e,f,g,h,i,j}}
> A0=array:from_orddict(lists:zip(NumList,AtomList),undef).
{array,10,10,undef,{a,b,c,d,e,f,g,h,i,j}}
> array:is_fix(A0).
false

Konwersja do listy lub słownika, upakowanie

> A1=array:reset(4,A0).
{array,10,10,undef,{a,b,c,d,undef,f,g,h,i,j}}
> A2=array:reset(8,A1).
{array,10,10,undef,{a,b,c,d,undef,f,g,h,undef,j}}
> A3=array:reset(9,A2).
{array,10,10,undef,{a,b,c,d,undef,f,g,h,undef,undef}}
> array:to_list(A3).
[a,b,c,d,undef,f,g,h,undef,undef]
> array:to_orddict(A3).
[{0,a},{1,b},{2,c},{3,d},{4,undef},{5,f},{6,g},{7,h},{8,undef},{9,undef}]
> array:sparse_to_list(A3).
[a,b,c,d,f,g,h]
> array:sparse_to_orddict(A3).
[{0,a},{1,b},{2,c},{3,d},{5,f},{6,g},{7,h}]

Tablice stałej długości

> A0=array:new(10).
{array,10,0,undefined,10}
> array:is_fix(A0).
true
> A1=array:from_list([a,b,c,d,e,f,g,h,i,j],0).
{array,10,10,0,{a,b,c,d,e,f,g,h,i,j}}
> array:is_fix(A1).
false
> array:size(A1).
10
> A2=array:fix(A1).
{array,10,0,0,{a,b,c,d,e,f,g,h,i,j}}
> array:is_fix(A2).
true

Operacje ta tablicach stałej długości

> catch array:get(20,A2).
{'EXIT',{badarg,[{array,get,2},...]}
> catch array:set(11,k,A2).
{'EXIT',{badarg,[{array,set,3},...]}
> A3=array:resize(20,A2).
{array,20,0,0,{{a,b,c,d,e,f,g,h,i,j},10,10,10,10,10,10,10,10,10,10}}
> array:set(11,k,A3).
{array,20,0,0,{{a,b,c,d,e,f,g,h,i,j},{0,k,0,0,0,0,0,0,0,0},...}}

Dynamiczne tablice

> A4=array:set(12,l,A1).
{array,13,100,0,{{a,b,c,d,e,f,g,h,i,j},{0,0,l,0,0,0,0,0,0,0},...}}
> array:size(A4).
13
> array:get(17,A4).
0
> A5=array:fix(A4).
{array,13,0,0,{{a,b,c,d,e,f,g,h,i,j},{0,0,l,0,0,0,0,0,0,0},...}}
> catch array:set(17,q,A5).
{'EXIT',{badarg,[{array,set,3},...]}}
> A6=array:relax(A5).
{array,13,100,0,{{a,b,c,d,e,f,g,h,i,j},{0,0,l,0,0,0,0,0,0,0},...}}
> array:set(17,q,A6).
{array,18,100,0,{{a,b,c,d,e,f,g,h,i,j},{0,0,l,0,0,0,0,q,0,0},...}}

Funkcjonały operujące na słownikach

> A0=array:from_orddict([{0,0},{2,2},{4,4},{5,5},{7,7},{9,9}],0).
> FoldFun=fun(I,X,Y) -> lists:concat(["(",X,"*",Y,")"]) end.
> MapFun=fun(I,X) -> I + X end.
> array:map(MapFun,A0).
{array,10,10,0,{0,1,4,3,8,10,6,14,8,18}}
> array:sparse_map(MapFun,A0).
{array,10,10,0,{0,0,4,0,8,10,0,14,0,18}}
> array:foldl(FoldFun,"1",A0).
"(9*(0*(7*(0*(5*(4*(0*(2*(0*(0*1))))))))))"
> array:sparse_foldl(FoldFun,"1",A0).
"(9*(7*(5*(4*(2*1)))))"
> array:foldr(FoldFun,"1",A0).
"(0*(0*(2*(0*(4*(5*(0*(7*(0*(9*1))))))))))"
> array:sparse_foldr(FoldFun,"1",A0).
"(2*(4*(5*(7*(9*1)))))"

Słowniki - moduły dict i orddict

Tworzenie słownika, operacje na kluczach

> dict:new().
{dict,0,16,16,8,80,48,
  {[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]},
  {{[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]}}}
> D0=dict:from_list([{a,[1]},{b,[2]},{c,[3,4]},{a,[5,6,7]}]).
> dict:to_list(D0).
[{a,[5,6,7]},{b,[2]},{c,[3,4]}]
> dict:size(D0).
3
> dict:is_key(2,D0).
false
> dict:is_key(a,D0).
true
> dict:fetch_keys(D0).
[a,b,c]

Pobieranie, wyszukiwanie, wstawianie, usuwanie

> D0=dict:from_list([{a,1},{b,2},{c,3},{d,4}]).
> dict:fetch(a,D0).
1
> catch dict:fetch("a",D0).
{'EXIT',{badarg,[{dict,fetch,["a",...]},...]}}
> dict:find(a,D0).
{ok,1}
> dict:find("a",D0).
error
> D1=dict:erase(c,D0).
> D2=dict:store(e,5,D1).
> D3=dict:store(b,6,D2).
> dict:to_list(D3).
[{a,1},{b,6},{d,4},{e,5}]

Dołączanie

> D0=dict:from_list([{a,1},{b,2},{c,3},{d,4}]).
> catch dict:append(a,7,D0).
{'EXIT',{badarg,[{erlang,'++',[1,[7]]},{dict,append_bkt,3},...]}}
> catch dict:append_list(b,[-1,-2],D0).
{'EXIT',{badarg,[{erlang,'++',[2,[-1,-2]]},{dict,app_list_bkt,3},...]}}
> D4=dict:from_list([{a,[1]},{b,[2]},{c,[3]},{d,[4]}]).
> dict:to_list(D4).
[{a,[1]},{b,[2]},{c,[3]},{d,[4]}]
> D5=dict:append(a,7,D4).
> D6=dict:append_list(b,[-1,-2],D5).
> dict:to_list(D6).
[{a,[1,7]},{b,[2,-1,-2]},{c,[3]},{d,[4]}]

Funkcjonały operujące na słownikach

> D0=dict:from_list([{"a",1},{"b",2},{c,3},{d,4}]).
> dict:to_list(dict:filter(fun(K,V) -> is_atom(K) and (V rem 2 == 0) end,D0)).
[{d,4}]
> dict:to_list(dict:map(fun(K,V) -> lists:concat([K,V]) end,D0)).
[{"b","b2"},{c,"c3"},{d,"d4"},{"a","a1"}]
> dict:fold(fun(Key,Val,Acc) -> Val + Acc end,0,D0).
10
> D1=dict:from_list([{a,1},{b,2},{c,3},{d,4}]).
> D2=dict:from_list([{c,5},{d,6},{e,7},{f,8}]).
> dict:to_list(dict:merge(fun(K,V1,V2) -> [V1,V2] end,D1,D2)).
[{a,1},{b,2},{c,[3,5]},{d,[4,6]},{e,7},{f,8}]

Aktualizacja wartości, łączenie słowników

> D0=dict:from_list([{"a",1},{"b",2},{c,3},{d,4}]).
> dict:to_list(dict:update(d,fun(V) -> V * 2 end,D0)).
[{"b",2},{c,3},{d,8},{"a",1}]
> catch dict:to_list(dict:update("d",fun(V) -> V * 2 end,D0)).
{'EXIT',{badarg,[{dict,update,["d",#Fun<erl_eval.6.13229925>,...]}]}}
> dict:to_list(dict:update("d",fun(V) -> V * 2 end,-4,D0)).
[{"b",2},{c,3},{d,4},{"d",-4},{"a",1}]
> dict:to_list(dict:update_counter(c,-3,D0)).
[{"b",2},{c,0},{d,4},{"a",1}]

Ciągi znakowe - moduł string

Długość, porównanie, tworzenie, zmiana wielkości znaków

> Chars = lists:seq( ''a,''z ).
"abcdefghijklmnopqrstuvwxyz"
> Digits = lists:seq( ''0,''9 ).
"0123456789"
> string:len( Chars ).
26
> string:equal( Chars,tl ( Chars ) ).
false
> string:chars( $.,20 ).
"...................."
> string:chars( $.,20,Chars ).
"....................abcdefghijklmnopqrstuvwxyz"
> string:copies( Digits,3 ).
"012345678901234567890123456789"
> string:to_upper( "Ala Ma 3 Koty" ).
"ALA MA 3 KOTY"
> string:to_lower( "Ala Ma 3 Koty" ).
"ala ma 3 koty"

Wyszukiwanie znaków i podciągów

> D2 = string:copies( lists:seq( ''0,''9 ),2 ).
"01234567890123456789"
> string:chr( D2,$a ).
0
> string:chr( D2,$0 ).
1
> string:rchr( D2,$7 ).
18
> string:str( D2,"456" ).
5
> string:rstr( D2,"456" ).
15
> string:span( ",,,...,.abcdef",",." ).
8
> string:cspan( "abcdef,,,...,.",",." ).
6

Podciągi i słowa

> CSV = " field 1; field 2; field 3; ...; field K".
" field 1; field 2; field 3; ...; field K"
> string:substr( D2,5 ).
"4567890123456789"
> string:substr( D2,7,17 ).
"67890123456789"
> string:words( CSV ).
9
> string:words( CSV,$; ).
5
> string:sub_word( CSV,4 ).
"2;"
> string:sub_word( CSV,100 ).
[]
> string:sub_word( CSV,2,$; ).
" field 2"

Obcinanie, rozbijanie na słowa, łączenie

> S1 = "     ala   ma kota   ".
"     ala   ma kota   "
> S2 = ".....ala;..ma;kota...".
".....ala;..ma;kota..."
> string:strip( S1 ).
"ala   ma kota"
> string:strip( S1,left ).
"ala   ma kota   "
> string:strip( S1,right ).
"     ala   ma kota"
> string:strip( S2,both,$. ).
"ala;..ma;kota"
> TL = string:tokens( S2,".;" ).
["ala","ma","kota"]
> string:join( TL," => " ).
"ala => ma => kota"

Justyfikacja

> S = lists:seq( ''a,''a + 10 ).
"abcdefghijk"
> string:left( S,74 ).
"abcdefghijk                                                               "
> string:left( S,74,$. ).
"abcdefghijk..............................................................."
> string:right( S,74 ).
"                                                               abcdefghijk"
> string:right( S,74,$. ).
"...............................................................abcdefghijk"
> string:centre( S,74 ).
"                               abcdefghijk                                "
> string:centre( S,74,$. ).
"...............................abcdefghijk................................"

Konwersje do liczb

> { _,Fs } = string:to_float ( "1.0-1.0e-1" ).
{1.0,"-1.0e-1"}
> { _,[ ] } = string:to_float ( Fs ).
{-0.1,[]}
> string:to_float ( "-1.5eX" ).
{-1.5,"eX"}
> string:to_float ( "3/2=1.5" ).
{error,no_float}
> string:to_float ( false ).
{error,not_a_list}
> { _,Is } = string:to_integer( "33+22" ).
{33,"+22"}
> { _,[ ] } = string:to_integer( Is ).
{22,[]}
> string:to_integer( "0.5" ).
{0,".5"}
> string:to_integer( "x=2" ).
{error,no_integer}

Wybrane moduły z biblioteki standardowej

Struktury danych

  • sets - funkcyjne zbiory
  • gb_sets - alternatywna implementacja zbiorów za pomocą drzew zbalansowanych
  • ordsets - uporządkowany zbiór
  • sofs - operacje na zbiorach
  • gb_trees - funkcyjne zbalansowane drzewa
  • queue - funkcyjna kolejka FIFO
  • proplists - listy własności

Grafy

  • digraph - funkcyjne grafy
  • digraph_utils - operacje na grafach

Operacje na ciągach znakowych

  • string
  • re
  • regexp
  • unicode
  • base64

Operacje matematyczne

  • math
  • random

Obsługa czasu

  • timer
  • calendar
 
erlang/stdlib.txt · Last modified: 2010/05/07 21:45 by Krystian Bacławski
 
Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution-Noncommercial-No Derivative Works 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki