Code Monkey home page Code Monkey logo

vital-design-by-contract's Introduction

๐Ÿ”ฎ vital-design-by-contract

Design by Contract in Vim script

Installation

Dependencies: https://github.com/vim-jp/vital.vim

vital-design-by-contract is an external module of vital.vim

NeoBundle 'vim-jp/vital.vim'
NeoBundle 'haya14busa/vital-design-by-contract'

Plugin 'vim-jp/vital.vim'
Plugin 'haya14busa/vital-design-by-contract'

Plug 'vim-jp/vital.vim'
Plug 'haya14busa/vital-design-by-contract'

Example

with https://github.com/haya14busa/vital-power-assert

let s:V = vital#of('vital')
let s:DbC = s:V.import('Vim.DbC')
execute s:V.import('Vim.PowerAssert').define('Assert')

function! s:flatten(list, ...) abort
  let limit = a:0 > 0 ? a:1 : -1
  let memo = []
  if limit == 0
    return a:list
  endif
  let limit -= 1
  for Value in a:list
    let memo +=
    \  type(Value) == type([]) ?
    \    s:flatten(Value, limit) :
    \    [Value]
    unlet! Value
  endfor
  return memo
endfunction

" s:__pre_{func}() will be called before {func} with a:var args as a:in
function! s:__pre_flatten(in) abort
  " input should be list
  Assert type(a:in.list) is# type([])
  " limit should be number
  Assert type(get(a:in, 1, 0)) is# type(0)
  " You can use a:in.var to access a:var
  echo '__pre_flatten : in : ' . string(a:in.list)
endfunction

" s:__post_{func}() will be called after {func} with a:var args as a:in and
" returned value as a:out
function! s:__post_flatten(in, out) abort
  " Each value of returned list should not be list
  for l:X in a:out
    Assert type(l:X) isnot# type([])
  endfor
  echo '__post_flatten: out: ' . string(a:out)
endfunction

" You can disable contract check for production if you want like this
if get(g:, 'pluginname_enable_dcb', 1)
  " Magic happens!
  execute s:DbC.dbc()
endif

echo s:flatten([[1], [2, [3], [[4]]]])
" =>
" __pre_flatten : in : [[1], [2, [3], [[4]]]]
" __pre_flatten : in : [1]
" __post_flatten: out: [1]
" __pre_flatten : in : [2, [3], [[4]]]
" __pre_flatten : in : [3]
" __post_flatten: out: [3]
" __pre_flatten : in : [[4]]
" __pre_flatten : in : [4]
" __post_flatten: out: [4]
" __post_flatten: out: [4]
" __post_flatten: out: [2, 3, 4]
" __post_flatten: out: [1, 2, 3, 4]
" [1, 2, 3, 4]

vital-design-by-contract's People

Contributors

haya14busa avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.