These are chat archives for ramda/ramda

10th
May 2017
Otto Nascarella
@ottonascarella
May 10 2017 20:06
guys...anyone up for some immutable-js-on-ramda-and-anywhere-fantasy-dwell?
https://github.com/ottonascarella/immutable-fantasy
PS: help is welcome (more than welcome...really)
Riderman de Sousa Barbosa
@Ridermansb
May 10 2017 23:03
Hi.. how can I extract group from regex and display on console?
I try this one..
const topic = 'robot/20:30:AA:13'
const topicWithRobotMAC = /^robot\/([a-zA-Z0-9]{2}:.+)/gmi;
const matchRobotMAC = R.match(topicWithRobotMAC, topic);
const sayMACAddress = mac => console.log('MAC Address is ' + mac);

R.ifElse(
  matchRobotMAC,
  R.tap(sayMACAddress, 'MAC ADDRESS HERE')
);
Brad Compton (he/him)
@Bradcomp
May 10 2017 23:08
You could wrap the when call in tap if you want the matches at the end
Riderman de Sousa Barbosa
@Ridermansb
May 10 2017 23:09
Your code shows undefined
should be show 20:30:AA:13
James Forbes
@JAForbes
May 10 2017 23:11
if you use global pattern matching you lose groups in js
the regex interface in js is so bad, I can't believe it sometimes
Brian McKenna
@puffnfresh
May 10 2017 23:12
mutable
even
James Forbes
@JAForbes
May 10 2017 23:12
but also, ifElse expected a function, but you're passing a fully evaluated value as the first arg
@puffnfresh is it mutable?
Brian McKenna
@puffnfresh
May 10 2017 23:12
yeah, real bad
James Forbes
@JAForbes
May 10 2017 23:13
what's an example?
Brad Compton (he/him)
@Bradcomp
May 10 2017 23:13
@ram-bot
const topic = 'robot/20:30:AA:13'
const topicWithRobotMAC = /^robot\/([a-zA-Z0-9]{2}:.+)/gmi;

pipe(
  match(topicWithRobotMAC),
  head,
  tap(when(Boolean, console.log))
)(topic)
ram-bot
@ram-bot
May 10 2017 23:13
'robot/20:30:AA:13'

output

'robot/20:30:AA:13'
Brad Compton (he/him)
@Bradcomp
May 10 2017 23:14
Though if you're returning the match I don't see why you'd need to log it out
Riderman de Sousa Barbosa
@Ridermansb
May 10 2017 23:16
I will exec a function, tap is just for test
still return robot/20:30:AA:13..
how can I return only MAC address ? Group 1 in regex,
I try to replace /gmi by /mi
Brian McKenna
@puffnfresh
May 10 2017 23:16
@ram-bot
const x = /./g;
console.log(x.exec("Hello"));
console.log(x.exec("Hello"));
console.log(x.exec("Hello"));
ram-bot
@ram-bot
May 10 2017 23:16
undefined

output

[ 'H', index: 0, input: 'Hello' ]
[ 'e', index: 1, input: 'Hello' ]
[ 'l', index: 2, input: 'Hello' ]
Brian McKenna
@puffnfresh
May 10 2017 23:16
never use regexes, ever
Brad Compton (he/him)
@Bradcomp
May 10 2017 23:17
Sanctuary has some more principled ways of dealing with RegExps
:point_up_2: That... is pretty bad
James Forbes
@JAForbes
May 10 2017 23:18
@puffnfresh wow!
I never use exec, so I hadn't encountered that!
Brad Compton (he/him)
@Bradcomp
May 10 2017 23:21
match will return the groups as an array of Maybes
Brad Compton (he/him)
@Bradcomp
May 10 2017 23:23
@ram-bot
S.match
ram-bot
@ram-bot
May 10 2017 23:23
match :: NonGlobalRegExp -> String -> Maybe { groups :: Array (Maybe String), match :: String }
Brad Compton (he/him)
@Bradcomp
May 10 2017 23:23
@ram-bot
const topic = 'robot/20:30:AA:13'
const topicWithRobotMAC = /^robot\/([a-zA-Z0-9]{2}:.+)/mi;

S.match(topicWithRobotMAC, topic)
ram-bot
@ram-bot
May 10 2017 23:23
Just({"groups": [Just("20:30:AA:13")], "match": "robot/20:30:AA:13"})
Brad Compton (he/him)
@Bradcomp
May 10 2017 23:24
@ram-bot
const topic = 'robot/20:30:AA:13'
const topicWithRobotMAC = /^robot\/([a-zA-Z0-9]{2}:.+)/gmi;

S.match(topicWithRobotMAC, topic)
ram-bot
@ram-bot
May 10 2017 23:24
Invalid value

match :: NonGlobalRegExp -> String -> Maybe { groups :: Array (Maybe String), match :: String }
         ^^^^^^^^^^^^^^^
                1

1)  /^robot\/([a-zA-Z0-9]{2}:.+)/gim :: RegExp, GlobalRegExp

The value at position 1 is not a member of ‘NonGlobalRegExp’.

See https://github.com/sanctuary-js/sanctuary-def/tree/v0.9.0#NonGlobalRegExp for information about the sanctuary-def/NonGlobalRegExp type.
James Forbes
@JAForbes
May 10 2017 23:30
That's an interesting interface, I would have just made it a maybe of a list of groups that is either Nothing when any group wasn't satisfied, or Just when they all were. I've never had a situation where I wanted partial matches, but that is probably useful in other cases I'm sure.
Riderman de Sousa Barbosa
@Ridermansb
May 10 2017 23:30
@Bradcomp yep..
const topicWithRobotMAC = /^robot\/([a-zA-Z0-9]{2}:.+)/mi;
const matchRobotMAC = R.match(topicWithRobotMAC);
const sayMACAddress = mac => console.log('MAC Address is ' + mac);

R.pipe(
  matchRobotMAC,
  nth(1),
  tap(sayMACAddress)
)('robot/20:30:AA:13')
James Forbes
@JAForbes
May 10 2017 23:32
Hey do you all ever model Loading and Saving in the type system in UI code? E.g. this record won't have an id if it hasn't been saved yet, and may only be an id if it loading (say from the URL). Or would you just opt for generic types like Maybe or Task?

I'm doing this thing at the moment as a thought experiment:

const $SaveableGroup =
    SaveableT(
        'Group'
        ,'group_'
        ,{ Existing:
            { group_id: $UUID
            , group_name: $.String
            , roles: $.Array($SaveableRole)
            , users: $.Array($User)
            }
        , New:
            { group_name: $.String
            , roles: $.Array($SaveableRole)
            , users: $.Array($User)
            }
        }
    )

const $LoadableT = (name, $T) =>
    Sum(
        'Loadable'+name
        ,{ Loading: {}
        , Loaded: { loaded: $T }
        }
    )

const $LoadableGroup = $LoadableT('LoadableGroup', SaveableGroup)

const a = $LoadableGroup.Loading()
const b = $LoadableGroup.Loaded( $SaveableGroup.NewOf({ group_name, roles, invites }) )

// getName:: LoadableGroup SaveableGroup -> Maybe String
const getName =
$LoadableGroup
  .case({
     Loaded: ({ group_name }) => Just(group_name)
     ,Loading: K(Nothing)
  })

getName( a ) //=> Nothing
getName( b ) //=> Just('Group Name')

Just a sketch, not real code.

James Forbes
@JAForbes
May 10 2017 23:41
I'm thinking modelling these states would allow for cleaner UI code, e.g. only having loading spinners in certain parts of the UI, but I'm not sure if its a good idea to model them so directly, maybe its better to just use existing standard types
The reason I'm modelling say Loading states, is that its not that I want to wait for a Task to resolve before using it, a loading state is valid on its own.