Signup Now
Results 1 to 2 of 2
  1. #1
    Banned
    Join Date
    Jul 2014
    Posts
    358
    Reputation
    83
    Rep Power
    0

    some functional programming in lua

    Comming from Scala back here, I thought I could benefit from recreating some of its nice api in lua. In progress of creating my new script I prepared some of it, that I needed for my new actions and stuff. It is not complete, it might have bugs, it may have stupid design decisions, it is not the best lua code ever, maybe is not the most performant but certainly seems to work and makes me more productive, so I decided to share that I have. I don't have time to explain what each function does, but I probably will update this code and this post over time.

    Some of key things are that you should treat all of those objects, i.e. List, Set, Map, Option, Pair as immutable. Each modification creates a new object. I also (what might be odd, that's why I mention it) changed the way you index the collection, it is now (index) instead of [index], so you can say

    PHP Code:
    local list = List.of(9992954)
    print(list(
    2)) -- prints 92
    local set 
    Set.of(1254)
    print(
    set(54), set(12)) -- prints truefalse
    local map 
    Map.of({k1 4k3 1})
    print(
    map('k3')) -- prints 1 
    Here is the code http://wklej.org/id/2661976/ You can for example place it as first script in persistent scripts and just use those functions.

    To not leave you without a clue on how to use it, I will post my simple open bp action that uses that stuff http://wklej.org/id/2495118/

    here is how to create a list of all monsters that are targeted in your script, excluding Others and Categories

    PHP Code:
    Monsters = List.ofSettingsEntry('Targeting/Creatures')
        :
    map(function(setting) return getsetting(setting'Name'end)
        :
    filter(function(name)
            
    local isCategory string.sub(name1string.len('Category')) == 'Category'
            
    return name ~= 'Others' and not isCategory
        end

    Here is how to easly get stack and no stack items from your looting, say that all items you want to deposit have 'd' category.

    PHP Code:
    local stacksnoStacks = List.ofLootingItems('d'):map(extract('id')):partition(function(id) return iteminfo(id).iscumulative end):unpack() 
    I am aware that I gave hardly any explanation but I plan to do it later. Maybe someone will make some use of it even now.
    Last edited by walukasz; 06-30-2016 at 10:35 AM.

  2. #2
    Wind Tester
    Join Date
    Dec 2013
    Location
    Warsaw, Poland
    Posts
    2,579
    Reputation
    149
    Rep Power
    27
    Really nice job, thanks for sharing

 

 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •