39 lines
2.6 KiB
Text
39 lines
2.6 KiB
Text
{def wrap2 // the function's name
|
||
|
||
{def wrap2.r // recursive "loop"
|
||
{lambda {:linewidth :a1 :spaceleft :a2}
|
||
{if {A.empty? :a1} // it's the end so ...
|
||
then {A.toS :a2} // ... return the sentence
|
||
else {let { {:linewidth :linewidth} // local variables
|
||
{:spaceleft :spaceleft} // lambdatalk has no closure
|
||
{:a1 :a1} // so we recall outers values
|
||
{:a2 :a2} // ... manually
|
||
{:word {A.first :a1}} // get the first word
|
||
{:length {W.length {A.first :a1}}} // compute its length
|
||
} {wrap2.r // do it again with
|
||
:linewidth // the linewidth unchanged
|
||
{A.rest :a1} // the rest of array
|
||
{if {> {+ :length 1} :spaceleft} // if ...
|
||
then {- :linewidth :length} // then update spaceleft and
|
||
{A.addlast! {br}:word :a2} // push a breakline and the word
|
||
else {- :spaceleft {+ :length 1}} // else update spaceleft and
|
||
{A.addlast! :word :a2}} }}}}} // push the word
|
||
|
||
{lambda {:linewidth :text} // a function with 2 arguments
|
||
{wrap2.r :linewidth // call the recursive "loop"
|
||
{A.rest {A.new :text}} // the input as an array minus first word
|
||
{- :linewidth {W.length {S.get 0 :text}}} // compute spaceleft
|
||
{A.new {S.get 0 :text}}}}} // the output as an array plus first word
|
||
-> wrap2
|
||
|
||
{wrap2 70 {text}} ->
|
||
Personne n’a sans doute oublié le terrible coup de vent de
|
||
nord-est qui se déchaîna au milieu de l’équinoxe de cette
|
||
année, et pendant lequel le baromètre tomba à sept cent dix
|
||
millimètres. Ce fut un ouragan, sans intermittence, qui dura
|
||
du 18 au 26 mars. Les ravages qu’il produisit furent
|
||
immenses en Amérique, en Europe, en Asie, sur une zone large
|
||
de dix-huit cents milles, qui se dessinait obliquement à
|
||
l’équateur, depuis le trente-cinquième parallèle nord
|
||
jusqu’au quarantième parallèle sud ! (L’île mystérieuse /
|
||
Jules Verne)
|