Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Merging

28 views
Skip to first unread message

WJ

unread,
Jul 24, 2016, 8:49:14 AM7/24/16
to
You have a list contain both strings and numbers.
Generate a new list in which the adjacent strings
have been concatenated.

R:

merge.strings = function(items) {
if (length(items) == 0) return(items)
rev(
Reduce(
function(xs, x){
if (is.numeric(x) || is.numeric(xs[[1]]))
c(x, xs)
else
c( paste0(xs[[1]],x), tail(xs,-1))},
tail(items,-1),
head(items,1)))
}

> str(merge.strings(list("0","1","2",3,4,"5","6",7,"8","9")))
List of 6
$ : chr "012"
$ : num 3
$ : num 4
$ : chr "56"
$ : num 7
$ : chr "89"

In Forth?

--
The goal is to meet the EU challenge of interracial marriage. It's not a
choice. It's an obligation. If voluntarism does not work for the republic,
then the state will move in with more coercive measures.
--- the Jew Nicolas Sarkozy (www.youtube.com/watch?v=K0hD7IffTJs)

Julian Fondren

unread,
Jul 24, 2016, 2:58:58 PM7/24/16
to
On Sunday, July 24, 2016 at 7:49:14 AM UTC-5, WJ wrote:
> You have a list contain both strings and numbers.
> Generate a new list in which the adjacent strings
> have been concatenated.
>

What horrible tutorial would you even get this toy exercise from?
You have so many mixed-type data structures, and you do stupid
things with them. A this point it's fun to imagine you getting a
real-world problem.

"Hey WJ, take this list of employee performance stats and write
something that'll show who our top performers are."

So you get this:

Week: 18 July
EMPLOYEE OFFICE THINGS-DONE HOURS-WORKED
Alice WA 123 40
Bob WA 147 40
Evelyn WA 124 45
Matthew TX 161 39
...

Which is a collection of uniform types. And then you have
calculated data for each type -- its +/- standard deviations of
things-done-per-hour above/below the average, for example. Which
would fit nicely into a collection of uniform types that have more
properties.

So naturally,

WJ: "Sure thing. I'll make a big heterogeneous list of
WA-NoStats-Employees and TX-NoStats-Employees and
WA-WithStats-Employees and then I'll put group statistics in the
same list as unique types with one value, e.g.

class geometric-average
1 floats buffer: value
end-class

..."

> R:
>
> merge.strings = function(items) {
> if (length(items) == 0) return(items)
> rev(
> Reduce(
> function(xs, x){
> if (is.numeric(x) || is.numeric(xs[[1]]))
> c(x, xs)
> else
> c( paste0(xs[[1]],x), tail(xs,-1))},
> tail(items,-1),
> head(items,1)))
> }
>

Are you happy with this code?

> > str(merge.strings(list("0","1","2",3,4,"5","6",7,"8","9")))
> List of 6
> $ : chr "012"
> $ : num 3
> $ : num 4
> $ : chr "56"
> $ : num 7
> $ : chr "89"
>
> In Forth?
>

: @+ ( a -- a' x ) dup cell+ swap @ ;
: $, ( a u -- ) dup , here swap dup allot move ;
: $+! ( a u a2 -- ) 2dup @+ + 2swap +! swap move ;

variable items
: item, ( x -- ) here items @ , items ! -1 , , ;
: append? ( -- f ) items @ dup if cell+ @ 0 >= then ;
: item ( "item" -- )
append? if
parse-name dup allot items @ cell+ $+!
else
here items @ , items !
parse-name $,
then ;

: .items ( -- )
items @ begin dup while
dup cell+ @+ dup 0< cr if drop ? else type then
@ repeat drop ;

item 0
item 1
item 2
3 item,
4 item,
item 5
item 6
7 item,
item 8
item 9

.items

Output:

89
7
56
4
3
012


-- Julian
0 new messages