November 28, 2020, 11:00:02 AM

Author Topic: [bmx] Lambda by Azathoth [ 1+ years ago ]  (Read 382 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bmx] Lambda by Azathoth [ 1+ years ago ]
« on: June 29, 2017, 12:28:39 AM »
Title : Lambda
Author : Azathoth
Posted : 1+ years ago

Description : This is like the Lambda found in Lisp, Python, Lua, etc.
Wrote it in afew hours and its likely not very speed or memory efficient, and probably has bugs. Not much comments either.
Currently only has Multiply and Addition Types but its easy to add more.

*LType is a wrapper like Type that can store Lambda and Strings/Numbers, though when the value of a Lambda function is retreived it gets invoked.
LType.Str and Num are stored and handled the same and could probably remove Num.
LType.Set will try to guess the passed object if t=none (default)


Code :
Code: BlitzMax
  1. Strict
  2.  
  3. Type Lambda
  4.  
  5.         Field args:TList=CreateList()
  6.  
  7.         Method PushArg(a:LType)
  8.                 args.AddLast(a)
  9.         EndMethod
  10.        
  11.         Method GetArg:LType(i)
  12.                 Return LType(args.ValueAtIndex(i))
  13.         EndMethod
  14.  
  15.         Method Invoke:LType()
  16.         EndMethod
  17. EndType
  18.  
  19. Type LAdd Extends Lambda
  20.  
  21.         Function Add:LAdd(x:LType,y:LType)
  22.                 Local n:LAdd=New LAdd
  23.                
  24.                 n.PushArg(x); n.PushArg(y)
  25.                 Return n
  26.         EndFunction
  27.        
  28.         Method Invoke:LType()
  29.                 Local r:LType=LType.Create(String(GetArg(0).ToDouble()+GetArg(1).ToDouble()),LType.Num)
  30.                
  31.                 Return r
  32.         EndMethod
  33. EndType
  34.  
  35. Type LMul Extends Lambda
  36.  
  37.         Function Mul:LMul(x:LType,y:LType)
  38.                 Local n:LMul=New LMul
  39.                
  40.                 n.PushArg(x); n.PushArg(y)
  41.                 Return n
  42.         EndFunction
  43.        
  44.         Method Invoke:LType()
  45.                 Local r:LType=LType.Create(String(GetArg(0).ToDouble()*GetArg(1).ToDouble()),LType.Num)
  46.                
  47.                 Return r
  48.         EndMethod
  49. EndType
  50.  
  51. Type LType
  52.         Const None=0
  53.         Const Lambda=1
  54.         Const Num=2
  55.         Const Str=3
  56.  
  57.         Field _data:Object
  58.         Field _type:Int
  59.        
  60.         Function Create:LType(value:Object=Null,t=None)
  61.                 Local n:LType=New LType
  62.                 n.Set(value,t)
  63.                 Return n
  64.         EndFunction
  65.        
  66.         Method Set(value:Object=Null,t=None)
  67.                 If t=None And value<>Null               ' Guess type
  68.                         If .Lambda(value)
  69.                                 _type=LType.Lambda
  70.                         ElseIf String(value)
  71.                                 _type=Str
  72.                         EndIf
  73.                 Else
  74.                         _type=t
  75.                 EndIf
  76.                 _data=value
  77.         EndMethod
  78.        
  79.         Method ToString:String()
  80.                 If _type=Num Or _type=Str
  81.                         Return String(_data)
  82.                 ElseIf _type=LType.Lambda
  83.                         Return .Lambda(_data).Invoke().ToString()
  84.                 EndIf
  85.         EndMethod
  86.        
  87.         Method ToDouble:Double()
  88.                 Return ToString().ToDouble()
  89.         EndMethod
  90.        
  91. EndType
  92.  
  93.  
  94. ' *** Test
  95.  
  96. Local test:Lambda=LMul.Mul(..
  97.         LType.Create("10"),LType.Create(LAdd.Add( LType.Create("1"),LType.Create("1") ))..
  98.         )
  99.  
  100. Print "10*(1+1)"                                                               
  101. Print test.Invoke().ToString()
  102. Print "--~n"
  103.  
  104. ' *** Test with 'variables'
  105.  
  106.  
  107. Local UserArg1:LType=New LType
  108.  
  109. test=LMul.Mul(..
  110.         UserArg1,LType.Create(LAdd.Add( LType.Create("1"),LType.Create("1") ))..
  111.         )
  112.                                                                
  113.  
  114. UserArg1.Set("100")
  115. Print "UserArg1=100"
  116. Print "UserArg1*(1+1)"
  117. Print test.Invoke().ToString()


Comments :


Warpy(Posted 1+ years ago)

 Very interesting!


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal