Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Post History

75%
+4 −0
Challenges Merge two strings

BQN, 18 bytesSBCS {⊑(⊑𝕨⊸⍷)¨⊸/∾⟜𝕩¨↑𝕨} Run online! A direct translation of Razetime's APL solution (my attempted improvement ⊣∾{⊢´/𝕨⊸«⊸≡¨↑𝕩}↓⊢ turns out to be not at all correct). The BQN solut...

posted 3y ago by Marshall Lochbaum‭

Answer
#1: Initial revision by user avatar Marshall Lochbaum‭ · 2021-04-29T16:58:47Z (almost 3 years ago)
# [BQN](https://mlochbaum.github.io/BQN/), 18 bytes<sup>[SBCS](https://github.com/mlochbaum/BQN/blob/master/commentary/sbcs.bqn)</sup>

```
{⊑(⊑𝕨⊸⍷)¨⊸/∾⟜𝕩¨↑𝕨}
```

[Run online!](https://mlochbaum.github.io/BQN/try.html#code=RuKGkHviipEo4oqR8J2VqOKKuOKNtynCqOKKuC/iiL7in5zwnZWpwqjihpHwnZWofQoKRsK0wqgg4p+oCiAgIkFCQ0RFRiLigL8iRUZHSEkiCiAgIkFBQUFBQSLigL8iQUFBQUFBQUEiCiAgIkFCQyLigL8iMTIzIgogICIi4oC/IkFCQ0RFIgogICJBQkNEIuKAvyJBQkNEIgogICIi4oC/IiIK4p+p)

A direct translation of Razetime's APL solution (my attempted improvement `⊣∾{⊢´/𝕨⊸«⊸≡¨↑𝕩}↓⊢` turns out to be not at all correct).

The BQN solution is much shorter mainly because it has [Prefixes](https://mlochbaum.github.io/BQN/doc/prefixes.html) (`↑`) built in. Being able to filter with `⊸/` (see [Before](https://mlochbaum.github.io/BQN/tutorial/combinator.html#before-and-after)) also helps a lot.

```
{⊑(⊑𝕨⊸⍷)¨⊸/∾⟜𝕩¨↑𝕨}  # Function with left argument 𝕨 and right argument 𝕩
               ↑𝕨   # All prefixes of 𝕨
           ∾⟜𝕩¨     # Append 𝕩 after each one
         ⊸/         # Filter by...
  (    )¨           #   On each string,
    𝕨⊸⍷             #     Where does 𝕨 appear as a substring?
   ⊑                #     But I only care if it's the first one
 ⊑                  # Then take the first
```

Put together, the pattern `⊑𝕨⊸⍷` tests if `𝕨` is a prefix of the argument.