7
\$\begingroup\$

Objective

Given a type signature of a C function pointer represented as a string, output the (fully) curried version of it, also as a string.

I/O format

It is assumed that:

  • There is at least one parameter.
  • The return type and the parameter types are titlecased alphabetic identifiers. (That is, those matching the regex [A-Z][a-z]*)
  • The input and the output have no whitespaces.

Otherwise flexible.

Examples

Input -> Output

Foo(*)(Foo) -> Foo(*)(Foo)
Foo(*)(Bar) -> Foo(*)(Bar)
Bar(*)(Foo,Bar) -> Bar(*(*)(Foo))(Bar)
Bar(*)(Foo,Bar,Baz) -> Bar(*(*(*)(Foo))(Bar))(Baz)
\$\endgroup\$
6
  • \$\begingroup\$ Can useless () exist in output? \$\endgroup\$ Commented Aug 26, 2024 at 0:50
  • 1
    \$\begingroup\$ @l4m2 No. Parameter-less functions are disregarded explicitly here. \$\endgroup\$ Commented Aug 26, 2024 at 0:54
  • 1
    \$\begingroup\$ I mean e.g. ((Foo)(*)(Foo)) \$\endgroup\$ Commented Aug 26, 2024 at 0:55
  • \$\begingroup\$ @l4m2 Still no. \$\endgroup\$ Commented Aug 26, 2024 at 0:56
  • \$\begingroup\$ Now go and write the curried function for a&b|(a|b)&c... \$\endgroup\$ Commented Aug 26, 2024 at 3:48

8 Answers 8

5
\$\begingroup\$

K (ngn/k), 34 bytes

Replace each , with ))( and repeat (* for number of , times.

{((2*#i)#"(*")/"(*"\"))("/i:","\x}

Try it online!

{((2*#i)#"(*")/"(*"\"))("/i:","\x}
                            ","\    Split by ","
                    "))("/          Join by "))("
               "(*"\                Split by "(*"
              /                     Join by
 ((2*#i)#"(*")                        "(*" repeated for 1 + number of "," times
\$\endgroup\$
4
\$\begingroup\$

Jelly, 21 bytes

iṂœṖjċ”,⁾(*ẋƊṣ”,j“))(

A full program that accepts a string and prints the result.

Try it online!

How?

iṂœṖjċ”,⁾(*ẋƊṣ”,j“))( - Main Link: list of characters, S
 Ṃ                    - minimum {S} -> '('
i                     - first index of {'('}
  œṖ                  - partition {S} before {that index}
            Ɗ         - last three links as a monad - f(S):
     ċ”,              -   count occurrences of ','
        ⁾(*           -   "(*"
           ẋ          -   repeat {"(*"} {count} times -> "(*(*(*...(*"
    j                 - join {Partitioned S} with {"(*(*(*...(*"}
             ṣ”,      - split at occurrences of ','
                j“))( - join with "))("
                      - implicit print    
\$\endgroup\$
3
\$\begingroup\$

Java, 70 bytes

s->s.replace("(*","(*".repeat(s.split(",").length)).replace(",","))(")

Try it online.

Explanation:

s->                        // Method with String as both parameter and return-type
  s.replace("(*",          //  Replace the "(*"
            "(*".repeat(   //  with itself repeated the following amount of times:
              s.split(",") //   Split the input-string on ","
               .length))   //   And get its length
   .replace(",",           //  And also replace all ","
            "))(")         //  with "))("
\$\endgroup\$
3
\$\begingroup\$

JavaScript (Node.js), 60 bytes

x=>x.replace(/\W(\w*)/g,(_,y)=>y?(s+=`)(${y})`,'(*'):s,s='')

Try it online!

-1B from Shaggy

\$\endgroup\$
1
  • 1
    \$\begingroup\$ I don't think you need the + after the \W. \$\endgroup\$ Commented Aug 26, 2024 at 10:07
2
\$\begingroup\$

Retina 0.8.2, 17 bytes

+`(\).+),
(*$1))(

Try it online! Link includes test cases. Explanation: Repeatedly replace each comma in turn from right to left with ))(, inserting an extra (* after the return type each time.

\$\endgroup\$
1
\$\begingroup\$

Charcoal, 19 bytes

≔⪪θ,η⪫⪪⪫η))(¦(*⭆η(*

Try it online! Link is to verbose version of code. Explanation: Port of @akamayu's K answer.

≔⪪θ,η

Split the input on ,. This saves a byte because otherwise extra separators would be needed between the literal strings.

⪫⪪⪫η))(¦(*⭆η(*

Join that split with ))(, then split again on (*, then join with (* repeated for each part of the original split.

\$\endgroup\$
1
\$\begingroup\$

05AB1E (legacy), 19 17 bytes

',¢F„(*x.;',…))(:

-2 bytes ('(…(*(.;„(*x.;) switching to the legacy version of 05AB1E (built in Python), where multiply on strings to repeat them works

Try it online or verify all test cases.

Explanation:

',¢               '# Count how many "," are in the (implicit) input-string
   F               # Pop and loop that many times:
                   #  (use the implicit input again for the first iteration)
    „(*            #  Push string "(*"
       x           #  Double it to "(*(*" without popping
        .;         #  Replace the first "(*" with "(*(*"
                :  #  And then replace all
          ',      '#  ","
            …))(   #  with "))("
                   # (after the loop, the result is output implicitly)
\$\endgroup\$
1
\$\begingroup\$

Japt, 20 bytes

r"%(%*"È+pUè,Ãr,"))(

Try it

r"%(%*"È+pUè,Ãr,"))(     :Implicit input of string U
r                        :Replace
 "%(%*"                  :  Regex /\(\*/
       È                 :  Pass match through the following function
        +p               :    Append itself repeated
          Uè,            :      Count of "," in U times
             Ã           :End replace
              r,"))(     :Replace commas with parentheses
\$\endgroup\$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.