Word wrap: Difference between revisions
Content added Content deleted
No edit summary |
No edit summary |
||
Line 2,376: | Line 2,376: | ||
{def wrap1 |
{def wrap1 |
||
{lambda {:n} |
{lambda {:n} |
||
pre {@ style="width::npx; // the line width |
pre {@ style="width::npx; // the line width in pixels |
||
white-space:pre-wrap; "}}} // the word-wrap rule |
white-space:pre-wrap; "}}} // the word-wrap rule |
||
-> wrap1 |
-> wrap1 |
||
{{wrap1 |
{{wrap1 500} {text}} |
||
-> |
-> |
||
Personne n’a sans doute oublié le |
Personne n’a sans doute oublié le terrible coup de vent de nord-est |
||
⚫ | |||
terrible coup de vent de nord-est qui se déchaîna |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
millimètres. Ce fut un ouragan, sans |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
obliquement à l’équateur, depuis le |
|||
trente-cinquième parallèle nord jusqu’au |
|||
⚫ | |||
⚫ | |||
</lang> |
</lang> |
||
Line 2,401: | Line 2,396: | ||
A translation from the Kotlin entry: |
A translation from the Kotlin entry: |
||
<lang scheme> |
<lang scheme> |
||
{def wrap2 // the function's name |
|||
{def wrap2 |
|||
{def wrap2.r |
|||
{def wrap2.r // recursive "loop" |
|||
{lambda {:linewidth : |
{lambda {:linewidth :a1 :spaceleft :a2} |
||
{if {A.empty? :arr} |
|||
{if {A.empty? :a1} // it's the end so ... |
|||
then {S.rest :sb} |
|||
then {A.toS :a2} // ... return the sentence |
|||
else {let { {:linewidth :linewidth} |
|||
else {let { {:linewidth :linewidth} // local variables |
|||
{:spaceleft :spaceleft} |
{: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 |
|||
{lambda {:linewidth :text} |
|||
{A.addlast! {br}:word :a2} // push a breakline and the word |
|||
{wrap2.r :linewidth |
|||
else {- :spaceleft {+ :length 1}} // else update spaceleft and |
|||
{A.new :text} |
|||
{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 |
||
{wrap2 70 {text}} -> |
{wrap2 70 {text}} -> |
||
Personne n’a sans doute oublié le terrible coup de vent de |
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 |
nord-est qui se déchaîna au milieu de l’équinoxe de cette |
||
pendant lequel le baromètre tomba à sept cent dix |
année, et pendant lequel le baromètre tomba à sept cent dix |
||
un ouragan, sans intermittence, qui dura |
millimètres. Ce fut un ouragan, sans intermittence, qui dura |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
</lang> |
</lang> |
||