In our last guide, we covered the basics of variables and types — the nouns of our programming language. Now, it’s time to learn the verbs. We’re going to give our program a brain and muscles by introducing logic, repetition, and reusable code blocks.
Press enter or click to view image in full size
By the end of this tutorial, you’ll be able to control how your program runs, making it a dynamic and powerful tool. Let’s dive into conditionals, loops, and functions in Object Sense (OSE).
Making Decisions: Conditional Logic with if and switch
Conditional statements allow your program to make choices and execute different code based on whether a condition is true or false.
The most common way to do this in OSE is with an if/elseif/else block.
Here’s a practical example:
if {expr}
" todo
elseif {expr}
" todo
else
" todo
endif
For situations with many possible choices, a switch statement can be a cleaner alternative to a long chain of if/elseif statements.
let value = 0
if value >= 1
echo "value >= 1"
elseif value
echo "value < 1 && value not 0"
else
echo "value is 0"
endif
Switch type(object) ==
Case v:t_string call object.StaticHello("Hello string.")
Case v:t_dict call object.StaticHello("This is a dictionary.")
Default echo "nothing is matched"
Automating Repetitive Tasks: An Intro to Loops
Loops are your best friend for avoiding repetitive work. They allow you to execute the same block of code multiple times, saving you time and making your code much cleaner.
The for loop
The for loop is perfect for iterating over every item in a collection.
You can loop through a List:
let list = [0, 1, 2, 3, 4]
for item in list
echo item
endfor
And you can loop through a Dictionary to get both the key and the value:
let dict = {'x':1, 'y':2}
for [key,val] in items(dict)
echo key . '=>' . val
endfor
The while loop
A while loop continues to run as long as a certain condition is true.
let i = 0
while i < 10
if i == 5
echo "end loop"
break
endif
echo i
let i += 1
endwhile
You can use keywords like break (to exit the loop entirely) and continue (to skip to the next iteration) to control the flow.
Packaging Your Logic: Creating Reusable Functions
Functions are the key to organizing your code. They allow you to group a set of instructions into a reusable, named block.
Defining and Calling Functions
Here’s the basic syntax for defining a function. The ! indicates a global function.
function! FuncName(args?) dict?
...
endfunction
function! Sum(x, y)
return a:x + a:y
endfun
let sum = Sum(n1, n2) "Success
Lambdas for Quick, Anonymous Functions
For simple, one-line functions, you can use a lambda expression.
let RectSize = { rect -> rect.w * rect.h }
echo RectSize( {"w":100,"h":200} )
Closures for State-aware Functions
OSE supports closures, which are functions that remember the environment in which they were created. This is a powerful feature for creating functions that
function! Counter()
let count = 0
function! Increment() closure
let count += 1
return count
endfun
return funcref('Increment')
endfun
Putting It All Together
Let’s combine these concepts into one final example that processes a list of numbers.
function! ProcessNumbers(numbers)
let result = 0
for num in a:numbers
if num > 0
let result += num
endif
endfor
return result
endfun
let numbers = [1, -2, 3, -4, 5]
This simple function uses a loop to iterate, a conditional to make a decision, and is itself a reusable block of code.
Once you’re comfortable with conditionals, loops, and functions, you have the complete toolkit to write real, functional programs in ObjectSense. Happy coding!
Top comments (0)