Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
    Galen Ivanov
    @GalenIvanov
    Speaking of alternatives of regex, has someone tried Bracmat? It's about pattern matching in general, not only in text :
    Bracmat is a computer programming language designed for analysis and manipulation of complex data, be it evaluation of algebraic expressions, parsing natural language, validation of HTML or automatic chaining multi-facetted webservices into a workflow. In several projects, Bracmat has been combined with other programming languages to take care of high level activities that otherwise would be very time consuming to implement.
    I spent some time reading the docs, but the terse symbolic notation turned out to be beyond my grasp and my array languages experience.
    rseger
    @rseger

    this quote beats Perl in terms of line noise was what turned me off it. Line noise is the wrong sin, in my oh so humble opinion, to be focused on with perl. And you can see the results in the syntax they've chosen. They seem to be under the inaccurate impression that optimizing character count is somehow a good thing. Those days have long since passed. The correct optimization is for the maintainer of a codebase. You can certainly swing too verbose but the last thing I think when reading perl is "there's too much information in this line of code"

    that said, I was saddened to see it. Their goals and ideas seem to be very solid. I just wish they were in a more maintainable package.

    hiiamboris
    @hiiamboris
    Perl is one of the best. I wonder if such code is possible in Bracmat.
    rseger
    @rseger
    it truly is a marvel
    Galen Ivanov
    @GalenIvanov
    @rseger Yes, I agree!
    @hiiamboris I don't know Perl, but I think Raku (formerly Perl 6) have taken steps in the right direction - at least the (golfed) code I have seen in Code Golf & Coding Challenges on StackExchange looks much better than Perl.
    Gregg Irwin
    @greggirwin

    That Perl stuff is astounding. True artistry. Bracmat is an interesting find. Skimming the docs, it brings to my mind how important foundational principles are. His are quite specific and lead to design choices and a user experience that make it interesting (e.g. no subtraction, no named fields). If it empowers people, that's what DSLs are for, but it isn't clicking for me at a glance.

    There's an informal grammar in the PDF, but I can't copy it out to see how hard it might be to normalize. Not worth downloading and trying locally in PDFElement right now. If @GalenIvanov wants to reach out and see if the author can use the free version of DG to make a diagram for it, we'll render him one without watermarks.

    Galen Ivanov
    @GalenIvanov
    @greggirwin That's an interesting idea - yes, I'll try to contact him with your suggestion.
    Gregg Irwin
    @greggirwin
    :+1:
    Toomas Vooglaid
    @toomasv
    Bracmat grammar (for visualisation only):
    input: [opt expression opt [";" input]]
    expression:   [
     white-space expression white-space
     | opt prefixes "(" expression ")"
     | leaf
     | expression binop expression
    ]
    leaf: [opt prefixes atom-or-nil]
    atom-or-nil: [atom | nil]
    atom: [{"} string {"} | string]
    string: [character opt string]
    character: compose [(charset [not #"\" #"^""]) | spec]
    spec: compose ["\" (charset {abtnvfr"\})]
    nil: [none]
    binop: compose [(charset "=.,|&:+*^'$_") | white-space | "\L" | "\D"] 
    prefixes: [prefix opt prefixes]
    prefix: compose [(charset "[~/#<>%@`?!") | "!!"]
    white-space: charset " ^-^/^L"
    Gregg Irwin
    @greggirwin
    Thanks @toomasv !
    Toomas Vooglaid
    @toomasv
    :)
    Gregg Irwin
    @greggirwin
    bracmat.png
    @GalenIvanov feel free to pass that along to the author. If they want to use it, ask if they'll give us credit and point to https://www.redlake-tech.com/products/diagrammar-for-windows/. You can send them the working grammar from @toomasv as well.
    hiiamboris
    @hiiamboris
    the image doesn't zoom for me (says files.gitter.im inaccessible, but ping works) :/
    Gregg Irwin
    @greggirwin
    Technology is hard it seems. :^\
    yc
    @cloutiy
    Is diagrammar available for linux?
    Gregg Irwin
    @greggirwin
    Not at this time.
    Dmitry Bubnenkov
    @bubnenkoff

    Could anybody help me to adopt this code to output result in single quotes: like: '.
    I tied to write big hack with replace "{" but it produce a lot of errors.

    block-to-comma-list: function[str-blk] [
        str: none
        rule: [collect any [s: skip keep (mold s/1) [end | keep (", ")]]]
        str: head insert next copy "()" rejoin parse str-blk rule
        str: replace/all str "none" "NULL"
    
        print "-------"
        probe str
    ]
    
    block-to-comma-list ["25.22.11" "aa aaa aaaaa" {some string with quotes "Microsoft", "Apple", "HP"} "1.00" none]

    The result should look like as SQL insert VALUES. They are single quoted.

    Gregg Irwin
    @greggirwin
    You should just be able to add the single quotes at the head and tail of each string.
    Dmitry Bubnenkov
    @bubnenkoff
    Do not understand moment where it should be added(
    Gregg Irwin
    @greggirwin
    I don't remember SQL rules for embedded quotes. What should the "some string..." look like when done?
    Dmitry Bubnenkov
    @bubnenkoff
    VALUES(1 'aa', 'some "name"', 123, NULL)
    the quotes should look like this
    Gregg Irwin
    @greggirwin
    If just enclosing each string works, this should do it:
    enclose: func [
        "Returns a string with leading and trailing values added."
        str [any-string!]   "(modified)"
        val [char! series!] "A single value can be used in the series"
    ][
        rejoin either char? val [ [val str val] ][
            [form first val  str  last val]
        ]
    ]
    ;enbrace:   func [s] [enclose s "{}"]
    ;enbracket: func [s] [enclose s "[]"]
    ;enparen:   func [s] [enclose s "()"]
    ;enquote:   func [s] [enclose s {"}]
    ;entag:     func [s] [enclose s "<>"]
    
    block-to-comma-list: function[str-blk] [
        str: none
        rule: [collect any [set s string! keep (enclose s {'}) [end | keep (", ")]]]
        str: head insert next copy "()" rejoin parse str-blk rule
        str: replace/all str "none" "NULL"
    
        print "-------"
        probe str
    ]
    
    block-to-comma-list ["25.22.11" "aa aaa aaaaa" {some string with quotes "Microsoft", "Apple", "HP"} "1.00" none]
    Dmitry Bubnenkov
    @bubnenkoff
    Wow! Thanks! I need to check it on real data!
    Dmitry Bubnenkov
    @bubnenkoff
    block-to-comma-list ["25.22.11" "aa aaa aaaaa" "1.00" none]
    
    == "('25.22.11', 'aa aaa aaaaa', '1.00', )"
    it loosing none (that should be renamed to NULL)
    Gregg Irwin
    @greggirwin
    So you'll need to add a rule for | none! as an alternate. I missed that when changing to the string! check.
    Then you can remove your last replace call.
    Dmitry Bubnenkov
    @bubnenkoff
    I put none! in wrong place?
    rule: [collect any [set s string! keep (enclose s {'}) [end | keep (", ") | none!]]]
    After one year this string is look magic for me(
    Gregg Irwin
    @greggirwin
    Well, you're not doing anything when you see the none. You want to keep NULL in that case, right?
    Dmitry Bubnenkov
    @bubnenkoff
    yes
    To get:
    == "('25.22.11', 'aa aaa aaaaa', '1.00',  NULL)"
    Gregg Irwin
    @greggirwin
    So what do you think you need to do when you hit the none! rule? (hint: look at the string! rule)
    Dmitry Bubnenkov
    @bubnenkoff
    I tried different combination of keep none! and keep 'none do not work. Rule hard for me
    Gregg Irwin
    @greggirwin
    enclose: func [
        "Returns a string with leading and trailing values added."
        str [any-string!]   "(modified)"
        val [char! series!] "A single value can be used in the series"
    ][
        rejoin either char? val [ [val str val] ][
            [form first val  str  last val]
        ]
    ]
    ;enbrace:   func [s] [enclose s "{}"]
    ;enbracket: func [s] [enclose s "[]"]
    ;enparen:   func [s] [enclose s "()"]
    ;enquote:   func [s] [enclose s {"}]
    ;entag:     func [s] [enclose s "<>"]
    
    block-to-comma-list: function[str-blk] [
        str: none
        rule: [
            collect any [
                set s string! keep (enclose s {'})
                | 'none keep (" NULL")   ; change to none! if it's a real none! value
                [end | keep (", ")]
            ]
        ]
        str: head insert next copy "()" rejoin parse str-blk rule
        str: replace/all str "none" "NULL"
    
        print "-------"
        probe str
    ]
    
    block-to-comma-list ["25.22.11" "aa aaa aaaaa" {some string with quotes "Microsoft", "Apple", "HP"} "1.00" none]
    Dmitry Bubnenkov
    @bubnenkoff
    It do not add , before last NULL, if to make it (", NULL") it would issue on first none
    block-to-comma-list [none "25.22.11" "aa aaa aaaaa" {some string with quotes "Microsoft", "Apple", "HP"} "1.00" none]
    Gregg Irwin
    @greggirwin
    I leave the details to you. :^)
    Dmitry Bubnenkov
    @bubnenkoff
    Ok! Thanks!
    Dmitry Bubnenkov
    @bubnenkoff

    @greggirwin It's seems that I did it. The yesterday rule was to hard for me and I was not able to fix it. So I did next:

    enclose: func [
        "Returns a string with leading and trailing values added."
        str [any-string!]   "(modified)"
        val [char! series!] "A single value can be used in the series"
    ][
        rejoin either char? val [ [val str val] ][
            [form first val  str  last val]
        ]
    ]
    
    data: [none "25.22.11" 44 "aa aaa aaaaa" {some string with quotes "Microsoft", "Apple", "HP"} "1.00" none]
    
    result: ""
    
    count: 0
    foreach el data [
          count: count + 1
          if (string? el) [el: enclose el [{'}] "," ]
          if (el = 'none) [el: "NULL"]
          append result el
    
          if (count < length? data) [ ; prevent adding at end
          append result ", " 
          ]
    ]
    insert result "("
    append result ")"

    output:

    == {(NULL, '25.22.11', 44, 'aa aaa aaaaa', 'some string with quotes "Microsoft", "Apple", "HP"', '1.00', NULL)}
    Toomas Vooglaid
    @toomasv
    @bubnenkoff
    SQLify: func [data][
        head insert next copy "()" rejoin parse data [
            collect [
                any [[
                      ['none | none!] keep ("NULL") 
                    | ahead string! keep ("'") keep skip keep ("'") 
                    | keep skip] 
                    [end | keep (", ")]
    ]]]]
    >> SQLify data
    == {(NULL, '25.22.11', 44, 'aa aaa aaaaa', 'some string with quotes "Microsoft", "Apple", "HP"', '1.00', NULL)}
    Galen Ivanov
    @GalenIvanov
    @toomasv I was wondering isn't it possible to change head insert next copy "()" with mold to-paren?
    Apparently not in this particular case :smile:
    Toomas Vooglaid
    @toomasv
    As to-paren loads string, so yes, i.e. no.
    Galen Ivanov
    @GalenIvanov
    :+1:
    Oldes Huhuman
    @Oldes
    @toomasv your solution will fail once there will be some string like "it's fail"
    Toomas Vooglaid
    @toomasv
    @Oldes I'm only giving pointers, not trying to develop THE solution :)
    Gregg Irwin
    @greggirwin
    @bubnenkoff not using parse is a great option at times.