Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 509675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:02:06+00:00 2026-05-13T07:02:06+00:00

I’m trying to write a small little scripting engine for a bullet hell game

  • 0

I’m trying to write a small little scripting engine for a bullet hell game and I would like to do it in F#. I wrote some C# code to conceptualize it, but I’m having trouble porting it to F#. The C# code is posted below, and I would like some help porting it to F#. I have a feeling the matching F# code will be significantly smaller. I’m open to any sort of creative solutions 🙂

interface IRunner
{
    Result Run(int data);
}

struct Result
{
    public Result(int data, IRunner next)
    {
        Data = data;
        Next = next;
    }
    public int Data;
    public IRunner Next;
}

class AddOne : IRunner
{
    public Result Run(int data)
    {
        return new Result(data + 1, null);
    }
}

class Idle : IRunner
{
    public Result Run(int data)
    {
        return new Result(data, null);
    }
}

class Pair : IRunner
{
    IRunner _one;
    IRunner _two;

    public Pair(IRunner one, IRunner two)
    {
        _one = one;
        _two = two;
    }

    public Result Run(int data)
    {
        var res = _one.Run(data);
        if (res.Next != null)
            return new Result(res.Data, new Pair(res.Next, _two));
        return new Result(res.Data, _two);
    }
}

class Repeat : IRunner
{
    int _counter;
    IRunner _toRun;

    public Repeat(IRunner toRun, int counter)
    {
        _toRun = toRun;
        _counter = counter;
    }

    public Result Run(int data)
    {
        var res = _toRun.Run(data);
        if (_counter > 1)
        {
            if (res.Next != null)
                return new Result(res.Data,
                            new Pair(res.Next,
                                new Repeat(_toRun, _counter - 1)));
            return new Result(res.Data, new Repeat(_toRun, _counter - 1));
        }
        return res;
    }
}

class Sequence : IRunner
{
    IEnumerator<IRunner> _runner;

    public Sequence(IEnumerator<IRunner> runner)
    {
        _runner = runner;
    }
    public Result Run(int data)
    {
        var res = _runner.Current.Run(data);
        bool next = _runner.MoveNext();
        if (res.Next != null)
        {
            return new Result(res.Data,
                        new Pair(res.Next, new Sequence(_runner)));
        }

        return new Result(res.Data, new Sequence(_runner));
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T07:02:07+00:00Added an answer on May 13, 2026 at 7:02 am

    Here’s something that’s almost a direct translation of the same solution strategy.

    That said, I think there may be a better/simpler representation choice, I’m still mulling it over.

    type Runner = int -> Result
    and Result = Result of int * option<Runner>
    
    let AddOne = fun x -> Result(x+1, None)
    
    let Idle = fun x -> Result(x, None)
    
    let rec Pair(r1,r2) = fun x ->
        match r1 x with
        | Result(data,None) -> Result(data, Some(r2))
        | Result(data,Some(next)) -> Result(data,Some(Pair(next,r2)))
    
    let rec Repeat r n = fun x ->
        if n = 0 then r x else
        match r x with
        | Result(data,None) -> Result(data, Some(Repeat r (n-1)))
        | Result(data,Some(next)) -> Result(data, Some(Pair(next, Repeat r (n-1))))
    

    EDIT

    Here’s another way that’s a little more refined… am still trying to see if there’s a good way to work in a “list”, since the results seem isomorphic to cons cells…

    type Runner = Runner of (int -> int * option<Runner>)
    
    let AddOne = Runner(fun x -> x+1, None)
    
    let Idle = Runner(fun x -> x, None)
    
    let rec Pair(Runner(r1),R2) = Runner(fun x ->
        match r1 x with
        | data,None -> data, Some(R2)
        | data,Some(next) -> data, Some(Pair(next,R2)))
    
    let rec Repeat (Runner(r) as R) n = Runner(fun x ->
        if n = 0 then r x else
        match r x with
        | data,None -> data, Some(Repeat R (n-1))
        | data,Some(next) -> data, Some(Pair(next, Repeat R (n-1))))
    

    EDIT

    One more version, it uses lists, but now I’ve a feeling for what’s weird here…

    type Runner = Runner of (int -> int * list<Runner>)
    
    let AddOne = Runner(fun x -> x+1, [])
    
    let Idle = Runner(fun x -> x, [])
    
    let rec Pair(Runner(r1),R2) = Runner(fun x ->
        match r1 x with
        | data,xs -> data, xs @ [R2]) // inefficient
    
    let rec Repeat (Runner(r) as R) n = Runner(fun x ->
        if n = 0 then r x else
        match r x with
        | data,xs -> data, xs @ List.init (n-1) (fun _ -> R)) // inefficient
    

    It’s almost just like an ‘Action queue’, a list of int->int functions. But each guy can produce some ‘suffix actions’ that run immediately after him (but before the remaining work in the would-be queue), and trying to maintain the ordering with a purely functional data structure is potentially inefficient (without the right tree/queue library at hand). It would be interesting to know how this will be used/consumed, as perhaps a small change there might allow for a completely different strategy.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 425k
  • Answers 425k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If you can use .Net 4.0, you can solve your… May 15, 2026 at 12:20 pm
  • Editorial Team
    Editorial Team added an answer There's no need to go to OpenXML here if you… May 15, 2026 at 12:20 pm
  • Editorial Team
    Editorial Team added an answer I changed my code to - <Button Cursor="Hand" HorizontalAlignment="Left" Margin="70,0,0,0"… May 15, 2026 at 12:20 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.